SetFocus apparently does not work

corentint

New Member
Joined
Jan 31, 2022
Messages
32
Office Version
  1. 365
Platform
  1. 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.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Would prevention not be better then a cure?
Code:
Private Sub VintageYear_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
 
Upvote 0
In the above message, change "then" to "than"
 
Upvote 0
What happened when you used the BeforeUpdate code?
 
Upvote 0
When the code within AfterUpdate is being executed, the textbox that triggered the event still has focus. And so SetFocus has not effect. It's only when it finishes executing the code that focus passes to the next control.
 
Upvote 0
Would prevention not be better then a cure?
Code:
Private Sub VintageYear_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
Thank you for your response.
I like to learn from others and my mistakes and this shows me another way of doing. So I tried your code, and sure enough, it will prevent a non numeric to be entered in the textbox.
But the problem remains entire still:
- The box goes empty (any letters are blanked out)
- And the box does not get a 0 value
- And the focus does not return to the box.
So I remain stuck with a user having to point the mouse and click the textbox to go at it again (or back tab); which for me is nothing to worry about, but not so for your average user....

Also, I am very curious, in your second reply, you change your answer:

"In the above message, change "then" to "than" "

What give? Than is a VBA operator and used in conjunction with Lesser, Greater etc. Not used with If, to my knowledge (which, granted, is imperfect)?
Thank you again
 
Upvote 0
What happened when you used the BeforeUpdate code?
Hello, and thank you for anwsering.

Actually, both codes with AfterUpdate and BeforeUpdate do nothing to prevent my problem, which is, if the user entered a non numeric value into the textbox, the message would say so and return de focus to the textbox - the focus never returns to the textbox.

Have a good day.
 
Upvote 0
What's wrong with your second BeforeUpdate code ? It works for me just fine.
 
Upvote 0
"Would prevention not be better then a cure?"

Should have mentioned that the "than/then" was meant for the above sentence mistake.

My apologies
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top