Full Version: Function for KeyDown event
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
StarsFan
Hello UA,

I have a number of cmdButtons on a Parent Form that changes the SourceObject of a subform. I would like to give the user the option of using the Right Arrow key to SetFocus on the subform. I know I can do this on each cmdButton using the KeyDown event but what I rather do is create a function to accomplish this so I don't have to code each and every button. My problem is I'm new to creating functions, subs and etc. Could I get someone to lend a hand in accomplishing this, please?

TIA,
Shane
StarsFan
Been searching but just not coming up with anything on my own. No one want to take a stab at it?
mhartman
Hello:

Try this:

Private Sub Form_Load()
Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyRight
MsgBox "Right arrow pressed"
forms!YourFormname.SetFocus
Case Else
Your code statements
End Select
End Sub


Regards

Mark
mhartman
Hello

The above code works if I place a textbox and command button on a form and press the right arrow.

The message box fired back and the focus move to a command button.

I hope this is what your after.

Regards

Mark
StarsFan
Thanks Mark. That is what I was after.

Shane
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.