ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   TextBox Focus And Active Cursor Movement In User Forms (https://www.excelbanter.com/excel-programming/433085-textbox-focus-active-cursor-movement-user-forms.html)

Rich Locus

TextBox Focus And Active Cursor Movement In User Forms
 
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?
--
Rich Locus
Logicwurks, LLC

Jacob Skaria

TextBox Focus And Active Cursor Movement In User Forms
 
---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
---------------
Jacob Skaria


"Rich Locus" wrote:

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?
--
Rich Locus
Logicwurks, LLC


Rich Locus

TextBox Focus And Active Cursor Movement In User Forms
 
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


"Jacob Skaria" wrote:

---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
---------------
Jacob Skaria


"Rich Locus" wrote:

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?
--
Rich Locus
Logicwurks, LLC



All times are GMT +1. The time now is 02:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com