在文本框中按Enter,然后在VBA中执行按钮功能 [英] Press enter in textbox and execute button function in VBA

查看:725
本文介绍了在文本框中按Enter,然后在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

我得到的是一个自发的错误,提示密码不正确.如果我进行调试,我会发现实际上txtPasswordnull,但是我只是在其中输入了文字!

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 the btnLogin_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆