Press Enter While In Textbox to Activate Command Button

thinair421

Registered User.
Local time
Today, 12:37
Joined
Jun 22, 2010
Messages
34
So I have a password textbox where the user enters their login password into. After they enter it, if they press enter it essentially "highlights", or gives focus (as if I tabbed over to the button) but doesn't actually click/activate it. Is there a way to have the 'Login' button activated when the user finishes typing password and presses enter? Thank you.
 
Set the button's DEFAULT property to YES. Then whenever the user presses their Enter key, no matter where they are on the form it will be just like clicking the button.

And just so you are aware, you can set a different button's CANCEL property to YES and then if you hit the ESC key it will run the click event of that button.
 
I've also been trying to figure this out for a while - Thanks Bob :-)
 
Found this in a forum:
(I didn't have a chance to test it yet)


you can capture the enter key in one textbox. try this:

Private Sub
myText_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
MsgBox "User hit the Enter key"
End If
End Sub
 
Yes, just tested it. It works perfect for me.

Code:
Private Sub txtTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
        MsgBox "User hit the Enter key"
    End If
End Sub


Here's are two lists of key codes:

http://msdn.microsoft.com/en-us/library/aa243025(v=vs.60).aspx
http://www.rolbe.com/2009/03/03/vb-keycode-chart/


And here is code to retrieve the keycode of the button you press:

Code:
Private Sub txtTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
    MsgBox ("The 'KeyCode' for the button you pressed = " & KeyCode & ".")
End Sub


EDIT:

Another reference on this subject:

http://www.access-programmers.co.uk/forums/showthread.php?t=176351
 
Last edited:
Found this in a forum:
(I didn't have a chance to test it yet)


you can capture the enter key in one textbox. try this:

Private Sub
myText_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
MsgBox "User hit the Enter key"
End If
End Sub

But if you have one button that you want to run its click event if you press the Enter Key while you are on the form and in ANY of the controls, then just use what I posted. There is no need for code. Just change the button's DEFAULT property from NO to YES and then, no matter where you are on the form, when you hit your Enter/Return key it will fire the code for that button.
 

Users who are viewing this thread

Back
Top Bottom