如何将Enter键连接到Submit按钮

Pow*_*ser 6 ms-access access-vba

我正在构建一个数据输入表单,其中包含一个运行脚本的"提交"按钮.这很好,但我想采取额外的步骤.

根据我对大多数Web表单的使用经验,无论表单中的光标位置如何,Enter键都可以绑定到某个命令.我希望将回车键绑定到此提交按钮.然后,无论当前哪个字段具有焦点,按Enter键将运行提交脚本.

那么,如何将Enter键与Submit按钮绑定?

我最好的猜测是为每个启用的文本框创建一个OnKeyPress事件.出于显而易见的原因,我希望有更好的方法.

Gor*_*son 12

如何将Enter键与Submit按钮绑定?

将.Default"提交"按钮的属性设置为Yes.这应该使Enter密钥在大多数情况下单击"提交"按钮.(同样,.Cancel命令按钮的属性会将其与Esc键关联.)


Joh*_*nes 5

You can set the OnKeyPress of the FORM to capture if it's the "Enter" key that's pressed, and if so then run the Submit script.

Key Preview=Yes

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn then
        ...Submit script
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

EDIT: You may need to play with that a bit, because you might actually want it on the KeyDown event. I think if you're on a TextBox then the Enter key won't work, so you might have to put it in KeyDown.