Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Enter key to Submit button

Tags:

vba

ms-access

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.

like image 571
PowerUser Avatar asked Sep 12 '13 15:09

PowerUser


People also ask

How do I get submit button to work in Enter key?

You don't need to use JavaScript to do submit button or input on entering key pressed. Just need to mark it up with type="submit" , and the other buttons mark them with type="button" .

How do I submit a form using Enter?

Enter = Submit If you have focus in the textbox and hit enter, the form will be submitted automatically. This behavior is consistent across all browsers and is known as implicit submission.

How do you trigger HTML button when you press Enter?

HTML Press the enter key inside the textbox to activate the button. Show activity on this post. By default, browsers will interpret Enter as submitting a form. <button> by default is type "submit" and accesses whatever is located in the form's action attribute (overridden if button's formaction is present).


3 Answers

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.

like image 120
Johnny Bones Avatar answered Nov 08 '22 19:11

Johnny Bones


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.)

like image 26
Gord Thompson Avatar answered Nov 08 '22 19:11

Gord Thompson


  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
like image 37
Jazz King Avatar answered Nov 08 '22 21:11

Jazz King