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:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Re: Userform Textbox Focus - Driving me crazy!

Instead of ScanInput.SetFocus, try using Keycode = 0
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

You. Are. A. Genius. I have been searching and searching on multiple sites for some solution to this problem, and that's the first time I've seen "KeyCode = 0"

Thank you!
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

Glad to help.
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

I had the same problem and KeyCode = 0 worked for me too but I don't have a clue of the logic behind it. :confused:
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

Yeah I am in the dark on why KeyCode 0 works also. Even on Microsoft's website for Key Codes, 0 is not even listed
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

Key code 0 is not a valid key, so setting the code to 0 effectively cancels the key that was pressed i.e. It's like the return wasn't pressed.
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

Interesting. I would have guessed an error would occur.
Do you know why trying to manually .SetFocus does not work?
 
Upvote 0
Re: Userform Textbox Focus - Driving me crazy!

Actually, the SetFocus did work. But with the KeyCode still set to vbKeyReturn, that keystroke was applied to the new textbox (ScanInput) rather than to .

It's as if you pressed Return twice, the first press sent the focus to ScanInput and the second returned the forcus to the original Textbox.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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