How to connect Enter key to Submit button
Asked Answered
N

3

9

I'm building a data entry form that includes a Submit button which runs a script. This works fine, but I'd like to take the extra step.

In my experience with most web forms, the Enter key can be tied to a certain command, regardless of the cursor location in the form. I'd like the enter key to be tied to this Submit button. Then, regardless of which field currently has the focus, hitting Enter will run the Submit script.

So, how do I tie the Enter key to the Submit button?

My best guess is to make an OnKeyPress event for every single enabled text box. For obvious reasons, I'm hoping there's a better way.

Nakasuji answered 12/9, 2013 at 15:52 Comment(0)
F
6

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

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.

Fr answered 12/9, 2013 at 16:11 Comment(2)
The Key Preview =True should be in the Load() event. Other than that, this works quite well using KeyPress.Nakasuji
@johny Bones Thanks for this answer. OnKeyPress worked for me in a text box (password field) using Access 2007.Confront
V
17

how do I tie the Enter key to the Submit button?

Set the .Default property of your "Submit" button to Yes. That should make the Enter key click the "Submit" button in most circumstances. (Similarly, the .Cancel property for a command button will associate it with the Esc key.)

Veneer answered 12/9, 2013 at 16:2 Comment(1)
This worked fine, except when the focus was on some other button (sorry, didn't think to mention that originally). I might end up using this instead of Johnny Bones's answer if the users prefer it.Nakasuji
F
6

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

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.

Fr answered 12/9, 2013 at 16:11 Comment(2)
The Key Preview =True should be in the Load() event. Other than that, this works quite well using KeyPress.Nakasuji
@johny Bones Thanks for this answer. OnKeyPress worked for me in a text box (password field) using Access 2007.Confront
Z
4
  1. Change the "default" property to true for the "OK" button
  2. Change the "cancel" property to true for the "Cancel" button
  3. No keypress monitoring needed
Zygospore answered 17/7, 2018 at 1:29 Comment(1)
With Access 2016 it is "Yes" instead of "true"Bitstock

© 2022 - 2024 — McMap. All rights reserved.