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.
 
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.
Thank you for answering. In fact, it appears that the event "beforeUpdate" keep the focus on the textbox even after the code execution finishes.
What induced me in error is that the blinking cursor disappears. Yet if I simply tab after the error is catched, because the focus is still on the Textbox, the error catching is triggered again. Blinking cursor or not, I must correct the Textbox. That is what I wanted.

Thanks all for the help.:)
 
Upvote 0
Solution

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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.
I have to correct myself. With the BeforeUpdate event, the focus stays on the Textbox after my code detect an input error and terminates. So this is what I want.
What induced me in error is that the blinking cursor disappears. Yet if I simply tab after the error is catched, because the focus is still on the Textbox, the error catching is triggered again. Blinking cursor or not, I must correct the Textbox. That is what I wanted.
 
Upvote 0
What's wrong with your second BeforeUpdate code ? It works for me just fine.
Thank you for answering. In fact, it appears that the event "beforeUpdate" keep the focus on the textbox even after the code execution finishes.
What induced me in error is that the blinking cursor disappears. Yet if I simply tab after the error is catched, because the focus is still on the Textbox, the error catching is triggered again. Blinking cursor or not, I must correct the Textbox. That is what I wanted.
Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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