Use Enter key for activating a command

  • Thread starter Thread starter RL
  • Start date Start date
R

RL

How do I make the Enter key activate a command button or a
macro from a form?

I have a text box on a form that feeds into a query. I have
a "Go" button that activates the macro that uses the query.

I want the user to be able to type in their text, and then
hit the Enter key, and have the Enter key activate the
macro. Right now they have to stop typing and click the
"Go" button with their mouse to move forward.

The EnterKeyBehavior Property does not do this, that I can
see. If I used a Parameter Query, the pop-up parameter box
would work this way, but I have other things going on in
this macro, and I really need the text to be entered in the
form.

Thanks for any help you can give me with this!
 
Couple of ways to do this, so you may want to see what works best for you.

Easiest is to use the OnGotFocus event of the command button (assuming that
the button is the next control in the Tab Order after the textbox) to run
the macro instead of the OnClick event of the command button.

Another way is the use the AfterUpdate event of the textbox to run the same
macro that is on the button's OnClick event.

Another is to actually trap for the use of the Enter key (using OnKeyPress
event) and then run the macro for the button's Click event.
 
How do I make the Enter key activate a command button or a
macro from a form?

I have a text box on a form that feeds into a query. I have
a "Go" button that activates the macro that uses the query.

I want the user to be able to type in their text, and then
hit the Enter key, and have the Enter key activate the
macro. Right now they have to stop typing and click the
"Go" button with their mouse to move forward.

The EnterKeyBehavior Property does not do this, that I can
see. If I used a Parameter Query, the pop-up parameter box
would work this way, but I have other things going on in
this macro, and I really need the text to be entered in the
form.

Thanks for any help you can give me with this!

Code the Control's AfterUpdate event:
DoCmd.RunMacro "MacroName"

See VBA help for the additional RunMacro arguments.
 
If you set the Default property of a command button (your "Go" button) to
Yes/True, then it's Click event will automatically fire when you press the
Enter key.

Likewise, if you set a command button's Cancel property to Yes/True, its
Click event will fire when you hit the ESC key.
 
Back
Top