Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

ms access - Press enter in textbox and execute button function in VBA

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...