How to recognize which key being pressed in VBA.

I have an unbound entry form in my Access 2013 application.  The user has to enter information about an item, submit the information and then enter information about another item into the same form.  This is a high volume data entry process.

Currently I have a 'Submit' button at the bottom of the form.  When the user clicks 'Submit', the data is edited and then written to the appropriate table.

All of the entry on the form can be done from the keyboard.  The way I have it set up now, after entering the data the user must take one hand off the keyboard and click the mouse on the 'Submit' button.  This is slowing them down and they have large data volumes to input.

I thought of having the 'Submit' button be the next tabstop after the last data field on the screen.  That doesn't work some of the time becasue some data is optional and they may or may not enter anything into the data field that precedes the SUbmit button in the tab order.

I've never worked with trapping keys strokes to initiate an action but I've seen Access application where it has been done.  The one I saw It was set up to submit the data on the screen by pressing the 'PageDown' key.

If I wanted to allow the user to press the 'Enter', 'PageDown'  (Or any key) key to submit the form, how and where in the VBA code  would I recognize that they pressed the 'Enter' key, so I could take the same actions as though they had clicked the 'Submit' button with the mouse?
LVL 1
mlcktmguyAsked:
Who is Participating?
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi Mick,

if you want to trigger command button with ENTER, that is easy ... set the Default property on the Other tab to Yes

btw, the Cancel property does the same thing for the ESCape key ...

have an awesome day,
crystal
0
 
NorieVBA ExpertCommented:
Why not add a shortcut for the Submit button?

You can easily do that by changing the caption of the button.

For example if the caption is currently Submit if you change it to &Submit then the user can use ALT+S to 'click' the button.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
thinking along the same lines, Norie ... added this but didn't get editing submitted before you commented ...

you can also set an ALT-hotkey up by prefacing a letter in the Caption property of the button with & 
for instance: Caption = &Submit means Alt-S will be hotkey
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ryan ChongCommented:
If I wanted to allow the user to press the 'Enter', 'PageDown'  (Or any key) key to submit the form, how and where in the VBA code  would I recognize that they pressed the 'Enter' key, so I could take the same actions as though they had clicked the 'Submit' button with the mouse? 

Open in new window

alternatively, try look for KeyDown or KeyPress event but you got to place that event in every controls.

Form.KeyDown Event (Access)
https://msdn.microsoft.com/en-us/vba/access-vba/articles/form-keydown-event-access
look for KeyCode = 13 for {Enter} key

Form.KeyPress Event (Access)
https://msdn.microsoft.com/en-us/vba/access-vba/articles/form-keypress-event-access
look for KeyAscii = 13 for {Enter} key

@crystal,
do you mean set Enter Key Behavior to Default?
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
@Ryan, no, there is a property on the Other tab of the property sheet called "Default". Its purpose is to identify a default command button for the Enter key.  Only one command button can be the default -- if another is set, the first one becomes No.  If, however, there is more than one command button and another one has the focus, pressing Enter will execute the Click event of that one instead.

CommandButton.Default Property (Access)
https://msdn.microsoft.com/en-us/VBA/Access-VBA/articles/commandbutton-default-property-access
0
 
Ryan ChongCommented:
@crystal

ah, ok. think we are referring to different controls :)
0
 
mlcktmguyAuthor Commented:
Thanks, I had no idea those options were available.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
you're welcome, mick ~ happy to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.