Full Version: Custom "on Event" Actions
UtterAccess Forums > Microsoft® Access > Access Forms
ScooterBruce
On specific forms, I would like add some specific command tied to key combinations.
For example, if the user presses Ctrl+M, it runs a specific report or opens a specific form.
Any suggestions on how to go about this?
theDBguy
Hi,
Have you tried using one of the form's "Key" events?
Just my 2 cents... 2cents.gif
missinglinq
As theDBguy suggested, the Form's KeyDown event is what you need here. Using it to utilize Alternate, Shift or Control in conjunction with another key is a little more complicated than using it with a single key, but not by much. The Constants for these three keys are
acAltMask
acShiftMask
acCtrlMask

First, you have to set the Form's KeyPreview Property to True. You can do it in the Properties Pane or by this code:
CODE
Private Sub Form_Load()
  Me.KeyPreview = True
End Sub

Then simply:
CODE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = acCtrlMask And KeyCode = vbKeyM Then
  MsgBox "Pop goes the weasel!"
  KeyCode = 0
End If
End Sub

Simply replace the line
MsgBox "Pop goes the weasel!"
with whatever code you need to run.
What you need to keep in mind, when doing this type of thing, is to not supersede Access native shortcuts that people are accustomed to using, such as <Ctrl> + <S>, which is used to Save.
Linq ;0)>
BananaRepublic
Note that if you want to assign shortcuts for whole application and not just a single form, creating a AutoKey macro may be a better approach as this is global to your application.
MMV.
missinglinq
True, but:
Linq ;0)>
ScooterBruce
Thank you so much!
orks like a charm
missinglinq
Glad we could help!
Linq ;0)>
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.