How to connect the Enter key for the Submit button

I am creating a data entry form that includes a submit button that launches a script. This works great, but I would like to take an extra step.

In my experience with most web forms, the Enter key can be tied to a specific command, regardless of the location of the cursor on the form. I would like the input key to be bound to this Submit button. Then, no matter which field currently has focus, pressing Enter will send the script.

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

My best guess is to make an OnKeyPress event for every included text field. For obvious reasons, I hope that there will be a better way.

+4
source share
2 answers

You can install OnKeyPress from FORM to grab if it pressed the Enter key, and if so, run the Submit script command.

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 a little because you really may want this in a KeyDown event. I think if you are in a TextBox, then the Enter key will not work, so you might have to put it in KeyDown.

+4
source

How do I bind the Enter key to the Submit button?

Set the .Default property of your .Default button Yes . In most cases, the Enter key should click the Submit button. (Similarly, the .Cancel property for the command button will associate it with the Esc key .)

+6
source

Source: https://habr.com/ru/post/1501895/


All Articles