View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Sam Kuo[_3_] Sam Kuo[_3_] is offline
external usenet poster
 
Posts: 86
Default SetFocus back to textbox after exiting

Thanks Dave.

For this particular textbox I have, it's probably easier for me to have the
validation done upon changing/exiting. But I can see the benefits of applying
your suggestion to my other controls input check.

Cheers
Sam


"Dave Peterson" wrote:

If you have an "ok" button on your userform, you could keep it disabled until
everything is the way you want.

Here's a sample that I saved from a previous post:

You could have a subroutine that checks all the input to see if it's valid
before you enable the combobox.

I made a small userform with a combobox, textbox and two commandbuttons. I
wanted to make sure that there was something in the textbox and something in the
combobox before enabling the commandbutton2 button.

Option Explicit
Private Sub ComboBox1_Change()
Call CheckInput
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
Call CheckInput
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
.ListIndex = -1
.Style = fmStyleDropDownList
End With
Me.CommandButton2.Enabled = False
End Sub
Private Sub CheckInput()

Dim Ok As Boolean

Ok = True
If Me.ComboBox1.ListIndex < 0 Then
Ok = False
ElseIf Me.TextBox1.Value = "" Then
Ok = False
End If

Me.CommandButton2.Enabled = Ok

End Sub


Sam Kuo wrote:

Hi Mike

Thanks for your reply.

I didn't use Cancel = True because it locks evertything else until the user
enters a value, but in my case, I'd like to allow the user to be able to go
away and look for the correct value if they have a false entry.

Is there another way around this problem?

Sam

"Mike H" wrote:

Hi,

If you use

Txt_StartValue.Value = ""
Cancel = True

The incorrect entry will be cleared and the focus will be set back to the
Textbox

Mike

"Sam Kuo" wrote:

Hi

I'm trying to set focus back to a textbox after exiting the textbox, if the
condition isn't met.

I found a similar example in an earlier thread by jimec74, titled "Using
SetFocus with Frames", which was said to work. But when I tested it myself in
Excel 2003, the SetFocus doesn't seem to fire (i.e. focus still jumps to the
next textbox). Is this just me?


Sub Txt_StartValue_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim Msg, Title, Response

If IsNumeric(Txt_StartValue.Value) = False Then
Msg = "You must enter a number"
Title = "Non-numeric Value"
Response = MsgBox(Msg,16,Title)
Txt_StartValue.SetFocus
End If

End Sub


--

Dave Peterson