corentint
New Member
- Joined
- Jan 31, 2022
- Messages
- 32
- Office Version
- 365
- Platform
- Windows
A good day to all,
After spending some hour looking this problem up, I still do not find a straight answer.
In a userform, I have several textboxes that the user fills out. Mostly a simple validation is done, trigered by the TextBox AfterUpdate event.
Here is the code for one of the textboxes:
_____________________________________________________________________________________
Private Sub VintageYear_AfterUpdate()
If Not IsNumeric(VintageYear.Value) Then
MsgBox "Only numerical data here, try again", vbCritical, "ERROR"
VintageYear.Text = vbNullString
VintageYear.SetFocus
Exit Sub
End If
End Sub
_________________________________________________________________________________________________________________
The validated content of the Textbox gets used elsewhere in a subroutine activated by a command button.
My question:
The focus fails to goback to VintageYear textbox. Why? Or what am I doing wrong?
I also tried, based on another answer obtained, to use this different event:
___________________________________________________________________________________________
Private Sub VintageYear_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsNumeric(VintageYear.Value) Then
MsgBox "Only numerical data here, try again", vbCritical, "ERROR"
VintageYear.Text = vbNullString
VintageYear.SetFocus '(although this appears to be superfluous here)
Cancel = True
Exit Sub
End If
End sub
___________________________________________________________________________________________________________________
Maybe the validation needs to be done outside the control event code? Seems like complicating things if so.
I also tried that route, no succes.
Thank you for helping.
After spending some hour looking this problem up, I still do not find a straight answer.
In a userform, I have several textboxes that the user fills out. Mostly a simple validation is done, trigered by the TextBox AfterUpdate event.
Here is the code for one of the textboxes:
_____________________________________________________________________________________
Private Sub VintageYear_AfterUpdate()
If Not IsNumeric(VintageYear.Value) Then
MsgBox "Only numerical data here, try again", vbCritical, "ERROR"
VintageYear.Text = vbNullString
VintageYear.SetFocus
Exit Sub
End If
End Sub
_________________________________________________________________________________________________________________
The validated content of the Textbox gets used elsewhere in a subroutine activated by a command button.
My question:
The focus fails to goback to VintageYear textbox. Why? Or what am I doing wrong?
I also tried, based on another answer obtained, to use this different event:
___________________________________________________________________________________________
Private Sub VintageYear_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsNumeric(VintageYear.Value) Then
MsgBox "Only numerical data here, try again", vbCritical, "ERROR"
VintageYear.Text = vbNullString
VintageYear.SetFocus '(although this appears to be superfluous here)
Cancel = True
Exit Sub
End If
End sub
___________________________________________________________________________________________________________________
Maybe the validation needs to be done outside the control event code? Seems like complicating things if so.
I also tried that route, no succes.
Thank you for helping.