Userform Textbox Focus

NickRed18

Board Regular
Joined
May 25, 2017
Messages
76
Hi everyone,

I am making a simple userform with a couple text boxes and 2 buttons. I am having a terrible time setting up the userform to set the focus of the cursor to my textbox after pressing enter - I want the focus set to this textbox every single time I press enter. This is my code, my userform's name is ScanningInterface. The issue is whenever I press [Enter] to submit my textbox input, the focus is shifted to one of my buttons right below it, even with all the .SetFocus commands I have!


'Whenever the userform pops up, sets focus - it works great right here
Private Sub UserForm_Activate()
ScanInput.SetFocus
End Sub




'Press enter - activate the sub Updatesheet from Module1
Private Sub ScanInput_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
ScanInput.SetFocus
If KeyCode = vbKeyReturn Then
ScanInput.SetFocus
Call Updatesheet
ScanningInterface.ScanInput.Text = ""
ScanInput.SetFocus
End If
ScanInput.SetFocus
End Sub


The focus is always shifted to selecting the button every time!!!
 
Last edited:
Re: Userform Textbox Focus - Driving me crazy!

I see what you're saying about the Return key being pressed twice, but just for clarification: The only items on the userform are the Textbox (ScanInput), and two buttons (AddNewItem and RemoveItem). The AddNewItem button is the one "stealing" the focus away after entering in my data into the textbox.
 
Last edited:
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Re: Userform Textbox Focus - Driving me crazy!

Think of it as a timing issue. The pressing the enter key down initiates your code which sets focus (although the focus hasn't actually left at that point) then when you release the key, focus shifts to the next control.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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