Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EnterKey to press button in VBA Userform

Tags:

excel

vba

I have a userform in Excel that asks for a username and password. Once you enter your password if you press Enter it just "selects" the next item which is the LogIn button, but it doesn't press it. You have to hit Enter again to actually press the button.

How can I make it so when the user presses enter on his keyboard the LogIn button is pressed and the code associated to is runs (Logincode_click)?

like image 792
user2385809 Avatar asked Oct 01 '13 20:10

user2385809


People also ask

How do I assign a button to a UserForm?

Add Buttons to the UserFormIn the Toolbox, click on the CommandButton button. On the UserForm, click at the bottom left, to add a standard sized CommandButton. With the new CommandButton selected, double-click on the Name property in the Properties window.


1 Answers

You could also use the TextBox's On Key Press event handler:

'Keycode for "Enter" is 13
Private Sub TextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
         Logincode_Click
    End If
End Sub

Textbox1 is an example. Make sure you choose the textbox you want to refer to and also Logincode_Click is an example sub which you call (run) with this code. Make sure you refer to your preferred sub

like image 171
Jacob D Avatar answered Sep 23 '22 01:09

Jacob D