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,188
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,188
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,188
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,697
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,442
Messages
5,340,310
Members
399,367
Latest member
ahmed elsaid

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top