TextBox Focus And Active Cursor Movement In User Forms

R

Rich Locus

I have a user form with many text boxes, most of which I edit for validity.
If someone enters an invalid number, all I want to do is MsgBox them, clear
the textbox, and return the blinking cursor to that same textbox... well, I
have tried everything. I can clear the textbox, but I still have to use my
mouse to get back to that textbox. I have used the SetFocus, keybd_event to
tab backwards, and other tricks, none of which completely worked. Ideas?
 
J

Jacob Skaria

---Why dont you try using the Exit event of the Textbox for
validations...Cancel ...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

'If validations fail
Cancel = True
End Sub


---If you are validating it from command button...what happens when you
setfocus and ExitSub

TextBox3.SetFocus: Exit Sub

If this post helps click Yes
 
R

Rich Locus

Jacob:

Your solution works VERY WELL!! The only downside that if I use "MsgBox" to
display the error, then after the message, the cursor is no longer in the
textbox and I have to click it.

HOWEVER, if I render the error message ON THE FORM, and not use MsgBox, it's
a perfect solution.

Thanks again for your time!

Here's the final code based on your suggestion:

Private Sub txtTotalUnits_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsNumeric(txtTotalUnits) Then
Me.txtTotalUnits = vbNullString
Me.txtErrorMessage.Value = "Total Units Must Be Numeric"
Cancel = True
End If
End Sub

Rich Locus
Logicwurks, LLC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top