在文本框中按回车并在 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
(可能也是因为 Tab 键顺序是这样的),但是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 时,表单会自动调用按钮单击事件处理程序.无需额外的键事件处理.
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.
这篇关于在文本框中按回车并在 VBA 中执行按钮功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!