Full Version: Open form on KeyPress
UtterAccess Forums > Microsoft® Access > Access Forms
kbrewster
I am trying to open a form when someone presses F12 when they are in a certain control.
CODE
Private Sub ManufacturerID_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF12 Then
        MsgBox "You pressed F12!!"
    End If
End Sub

The message box appears...but then an access form pops up asking what I want to save the form as!
fkegley
Try using an AutoKeys macro instead.

FOr I suppose you could set the KeyCode to 0:

Private Sub ManufacturerID_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF12 Then
KeyCode = 0
MsgBox "You pressed F12!!"
End If
End Sub
Edited by: fkegley on Wed May 31 14:03:42 EDT 2006.
freakazeud
Hi,
add:
eyCode = 0
Before your msgbox!
HTH
Good luck
kbrewster
I have different forms to open depending what field I am in. So for example...if the user presses 'F12' while in the Vendor control, frmVendorInfo will open....but then if the user presses 'F12' while in the Manufacturer control, frmManufacturer will open.
kbrewster
Thanks freakazeud...works great!!
freakazeud
You're welcome.
Good luck on future projects!
schroep
Just so you understand why this happened:
) F12 has a default behavior; it launches SAVE AS. You don't even need a form open; just try hitting F12 from anywhere in the main Access window.
2) So, to override that default behavior and replace it with your own, the last step is to set the keycode to 0, as the default behavior doesn't happen until AFTER the Key events are processed.
Note that, because of this, you may want to also use the KEYPRESS event of the FORM itself (after setting KeyPreview to YES) to trap any F12's that are pressed in controls where you don't have a custom handler, and kill the keypress by setting it to 0. Otherwise, your users might get used to hitting F12 and hit it in a place you didn't anticipate.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.