Calling VBA Function with Key Stroke

  • Thread starter Thread starter AlwaysFroosh!
  • Start date Start date
A

AlwaysFroosh!

I've tried searching the forum for this but I can't think of any keywords
that seem to bring up what I'm looking for, so I'll start a new post.

I'm wondering if there is a way to run a VBA function with a keystroke. For
instance, I'd like to be able to press Control+Shift+T or something and have
some code execute. Is this possible?
 
I've tried searching the forum for this but I can't think of any keywords
that seem to bring up what I'm looking for, so I'll start a new post.

I'm wondering if there is a way to run a VBA function with a keystroke. For
instance, I'd like to be able to press Control+Shift+T or something and have
some code execute. Is this possible?

Create an AutoKey macro.
See Access help
Autokey + Assign an action or set of actions to a key

Make sure the key combination you choose is not already assigned by
Access, i.e. Ctrl + C, Ctrl + P, etc.
 
I you wish to do this while inside a form you can use the KeyUp event to
check what keys are pressed and then react accordingly. Try something like


Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
' MsgBox KeyCode & vbCrLf & Shift

If KeyCode = 84 And Shift = 3 Then 'T=84 and Ctrl+Shift=6
'Call your Function here
End If

End Sub
 
Thanks fredg, exactly what I was looking for!


fredg said:
Create an AutoKey macro.
See Access help
Autokey + Assign an action or set of actions to a key

Make sure the key combination you choose is not already assigned by
Access, i.e. Ctrl + C, Ctrl + P, etc.
 
I've never used any of the key events on my forms. How are these supposed to
work? I've played with them a bit and I can't seem to get them to do anything.
 
The KeyUp for instance run when the user releases the active keys being
pressed. thus it is constantly be executed when ever the keyboard is being
used.

It return the KeyCode which represent which key was pressed (84 for T,...)
and Shift which indicates the status of certain 'special' keys such as CTRL,
ALT, Shift...
So by checking both the KeyCode and Shift you can trap the Sequence of keys
you are looking for to initiate your code/function.
 
I see, I see. I created a new form and yup, it works very well. That's
interesting, I'm sure I can put that to good use somehow in the future. For
this time though the AutoKeys works a little better because I want the
function to execute no matter what form is open.

Thanks for the help.
 
Back
Top