Run Code on Key Press Aka F12 or F5

  • Thread starter Thread starter The Boondock Saint
  • Start date Start date
T

The Boondock Saint

Is there a way you can run code on keypress
So that differant keys have differant code run if they are pressed?

Ive tried googling it but didnt really come up with anything.

A2000.....

Cheers All
The Saint
 
Create a macro named AutoKeys. (The name is important.)
Show the Macros Names column (View menu.)

In the Macro Names column, enter these 4 characters:
{F5}
for the F5 key. Then in the Action column, choose the action you want to run
when the keys are pressed.

Remember that this will execute anywhere in the application, regardless of
what form or report is open at the time. If you wanted something for one
specific form, you could set its KeyPreview property to Yes, and use its Key
Down event.
 
Thanks for that Allen,
In the action part.. is there anyway i can call on code from a form..
I have code like

Private Sub cmdclearaddcomm_Click()
addcomm = ""
End Sub

Which runs whenever i click the button cmdclearadd etc ....
Can I set it up to do similar ?
 
Use the RunCode action in your macro to call code.

There are several things to address:
- RunCode takes the name of a Function, not a Sub.
- The Private keyword prevents it being exposed as you need.
- Use the full name of the form's module when calling its Sub.

Steps to achieve this:

1. Open the form's module, and remove the Private from the line:
Private Sub cmdclearaddcomm_Click()
so it just reads:
Sub cmdclearaddcomm_Click()

2. Choose the Modules tab of the Database window, and click New.
In the new module enter:
Function CallClearAddComm()
Call Form_XXXX.cmdclearaddcomm_Click
End Function
replacing the XXXX with the name of your form.
Save with a name such as "Module1"

3. In your AutoKeys macro, choose the RunCode action.
In the lower pane, set the Function Name to:
=CallClearAddComm()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top