Userform TextBox.setfocus Problem

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I am running the following code for a userform when I press the "enter" key...

Code:
Sub txtCenterNumber_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal shift As Integer)
    Application.ScreenUpdating = False

    If KeyCode = vbKeyReturn Then
        btnCenterLookup_Click
    End If
End Sub

Right now, within the btnCenterlookup_Click there is a line of code at the end that tries to reset focus to the textbox referenced above (txtcenternumber.setfocus).

But, it's like that line of code doesn't run when I press the button. Everything else works fine, but the focus moves to the next control (within a frame) instead of staying on the textbox indicated above.

Basically, I want the focus ALWAYS to be set to that textbox unless the user physically goes somewhere else via mouse.

Any thoughts on why the .setfocus property isn't working?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Private Sub txtCenterNumber_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Application.ScreenUpdating = False
If KeyCode = vbKeyReturn Then
Application.OnTime Now, "resetfocus"
End If
End Sub

in a regular module:
Sub ResetFocus()
UserForm1.txtCenterNumber.SetFocus
End Sub
 
Upvote 0
The focus problem might be something to do with the control(s) being in a frame.

Mind you I don't quite see why the focus would move somewhere else if all you are doing is calling the click event of a button.

Anyway, I tried a couple of things and this seemed to work.
Code:
    Frame1.Controls("Textbox2").SetFocus
I used a very simple example with only a few textboxes and a couple of buttons.

I also changed the TabStop property to False for all but TextBox2.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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