Press enter in textbox and execute button function in VBA
Asked Answered
G

5

6

I have a login form to my database done in Access 2010 and using VBA code. I want to be able to press Enter on txtboxPassword and automatically execute btnLogin_Click event. I tried this:

Private Sub txtboxPassword_KeyDown(KeyCode As Integer, Shift As Integer)
 If KeyCode = 13 Then
    btnLogin_Click
 End If
End Sub

What I get is a self-made error saying Password is incorrect. If I debug I see that actually txtPassword is null, but I just typed the text in it!

However If I click the Login button with the mouse it works perfect. Why does vba behave like that? How can I do it to make it work?

NOTE I also tried with:

  • KeyPress: after I press Enter the focus goes to btnLogin (maybe also because the tab order is like that), but the btnLogin_Click event is not executed.
  • KeyUp: same like KeyPress.
Gaeta answered 1/4, 2014 at 17:31 Comment(2)
What do you mean by txtPassword is null? Textboxes have a Text property. But that one is usually an empty string but not null.Barcellona
When in debug mode I hover the mouse over txtboxPassword.Value I get nullGaeta
B
14

The buttons in Access have a property called Default (on the Other property page). If you set it to Yes the form calls the button click event handler automatically, when you press Enter. No need for additional Key-event handling.

There is also a Cancel property. If you set it to Yes for a button, the form activates it automatically when the user types the Esc-key. Very practical for Cancel buttons.

Barcellona answered 1/4, 2014 at 17:41 Comment(3)
I couldn't find that property either in the btnLogin or the form. Can you guide me a little bit more. Thanks.Gaeta
I am sorry, my answer was for VB.NET. I changed my answer for Access now. In Access you can set the Default property of the "OK" button.Barcellona
As referenced in this SO answer this also works in ExcelDeepdyed
C
2

to activate KeyCode, you need to set Key Preview Property on form to YES (it will be at the bottom)

If KeyCode = vbKeyReturn Then
    Your_fuction_here
End If
Crux answered 7/9, 2014 at 11:16 Comment(0)
P
0

Cant reproduce exactly your problem, but some time ago I had somewhat similar issue and solved it by adding:

Private Sub txtboxPassword_KeyDown(KeyCode As Integer, Shift As Integer)
  If KeyCode = 13 Then
     btnLogin_Click
     KeyCode.Value = 0
  End If
End Sub
Pustulate answered 1/4, 2014 at 17:55 Comment(2)
Didn't work. I got "Compiled error: Invalid qualifier" and KeyCode on KeyCode.Value is marked in blue.Gaeta
Yeah, it doesn't seem to like KeyCode.Value = 0. I presume the purpose of adding this is so the normal keypress action is absorbed?Macy
H
0

On the button you wish to action go to properties 'Other' and set the 'Default' to Yes. Then when you click on enter when in the text box it will action the button.

Higgins answered 17/5, 2017 at 16:18 Comment(0)
N
0

KeyPress rather than KeyDown works for me, and calling the _Click sub avoids sending triggers that may fire other events.

Private Sub txtboxPassword_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
         Call btnLogin_Click
    End If
End Sub
Numerical answered 22/3, 2018 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.