在文本框中按Enter,然后在VBA中执行按钮功能 [英] Press enter in textbox and execute button function in VBA
问题描述
我有一个登录表单,该表单是在Access 2010中使用VBA代码完成的.我希望能够在txtboxPassword
上按 Enter 并自动执行btnLogin_Click
事件.我试过了:
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
我得到的是一个自发的错误,提示密码不正确.如果我进行调试,我会发现实际上txtPassword
是null
,但是我只是在其中输入了文字!
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!
但是,如果我用鼠标单击登录"按钮,则效果很好.为什么vba会那样表现?我如何才能使其正常工作?
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?
注意,我也尝试过:
- KeyPress:在我按 Enter 后,焦点移到了
btnLogin
(可能还因为制表符顺序是这样的),但是未执行btnLogin_Click
事件. - KeyUp:与KeyPress相同.
- KeyPress: after I press Enter the focus goes to
btnLogin
(maybe also because the tab order is like that), but thebtnLogin_Click
event is not executed. - KeyUp: same like KeyPress.
推荐答案
Access中的按钮具有一个名为Default
的属性(在 Other 属性页上).如果将其设置为Yes
当您按下 Enter 时,该表单会自动调用按钮click事件处理程序.无需其他键事件处理.
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.
还有一个Cancel
属性.如果将其设置为按钮的Yes
,则当用户键入 Esc 键时,该表单会自动激活它.对于取消按钮非常实用.
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.
这篇关于在文本框中按Enter,然后在VBA中执行按钮功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!