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.
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" .
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.
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).
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With