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:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,174
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Re: Userform Textbox Focus - Driving me crazy!

Instead of ScanInput.SetFocus, try using Keycode = 0
 

NickRed18

Board Regular
Joined
May 25, 2017
Messages
76
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!
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,174
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Re: Userform Textbox Focus - Driving me crazy!

Glad to help.
 

Gashmore

New Member
Joined
Jun 20, 2017
Messages
6
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:
 

NickRed18

Board Regular
Joined
May 25, 2017
Messages
76
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,174
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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.
 

NickRed18

Board Regular
Joined
May 25, 2017
Messages
76
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?
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,690
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.
 

Forum statistics

Threads
1,078,137
Messages
5,338,458
Members
399,234
Latest member
WaddoAU

Some videos you may like

This Week's Hot Topics

Top