在文本框中按回车并在 VBA 中执行按钮功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22792836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:04:03  来源:igfitidea点击:

Press enter in textbox and execute button function in VBA

vbams-accessms-access-2010

提问by EricPb

I have a login form to my database done in Access 2010 and using VBA code. I want to be able to press Enteron txtboxPasswordand automatically execute btnLogin_Clickevent. I tried this:

我有一个在 Access 2010 中完成并使用 VBA 代码的数据库登录表单。我希望能够按EntertxtboxPassword,并自动执行btnLogin_Click事件。我试过这个:

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 txtPasswordis null, but I just typed the text in it!

我得到的是一个自制的错误,说密码不正确。如果我调试,我会看到实际上txtPasswordnull,但我只是在其中输入了文本!

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?

但是,如果我用鼠标单击“登录”按钮,它就完美无缺。为什么 vba 会这样?我该怎么做才能让它发挥作用?

NOTEI also tried with:

注意我也试过:

  • KeyPress: after I press Enterthe focus goes to btnLogin(maybe also because the tab order is like that), but the btnLogin_Clickevent is not executed.
  • KeyUp: same like KeyPress.
  • KeyPress:在我按下Enter焦点后btnLogin(可能也是因为 Tab 键顺序是这样的),但btnLogin_Click事件没有执行。
  • KeyUp:与 KeyPress 相同。

回答by Olivier Jacot-Descombes

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

Access 中的按钮有一个名为Default(在“其他”属性页上)的属性。如果将其设置为Yes表单,则在按下 时自动调用按钮单击事件处理程序Enter。不需要额外的键事件处理。

There is also a Cancelproperty. If you set it to Yesfor a button, the form activates it automatically when the user types the Esc-key. Very practical for Cancelbuttons.

还有一个Cancel属性。如果您将其设置Yes为按钮,当用户键入Esc-key时,表单会自动激活它。对于取消按钮非常实用。

回答by Dil Dilshan

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

要激活 KeyCode,您需要将表单上的Key Preview属性设置为YES(它将在底部)

If KeyCode = vbKeyReturn Then
    Your_fuction_here
End If

回答by Snowflake68

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.

在您希望操作的按钮上,转到属性“其他”并将“默认”设置为“是”。然后,当您在文本框中单击 Enter 时,它将操作该按钮。

回答by Henrik Erlandsson

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

KeyPress而不是 KeyDown 对我有用,并且调用 _Click 子可以避免发送可能触发其他事件的触发器。

Private Sub txtboxPassword_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
         Call btnLogin_Click
    End If
End Sub

回答by Lukas2

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