KeyDown Code Snippet

Steve R.

Retired
Local time
Yesterday, 21:15
Joined
Jul 5, 2006
Messages
5,683
One of the useful features of MS Access is the ability to capture the value of a key when it is depressed. Knowing the value of the key stroke, allows you to program what to do when certain values are entered. One such example using the tab key to move to certain controls. Bellow is a code snippet.

Since not all situations would apply, the code below would need to be modified for your particular situation. Using "Debug.Print "KeyCode: "; KeyCode" identifies the value of the key pressed.

Using the KeyDown event can also be used as a partial substitute for the input mask. For example, limiting data entry to numbers and the backslash for entering a short-date.

Code:
Private Sub Command0_KeyDown(KeyCode As Integer, Shift As Integer)
    Rem the use of NumLock key and the number pad may require special testing.
    Rem Debug.Print "KeyCode: "; KeyCode
    Select Case KeyCode
        Case 8 'Back Space
            Rem
        Case 9 'Tab Key
            Rem DoCmd.GoToControl "ProjectName01"
        Case 13 'Return Key
            Rem If Me.Page28.Visible Then DoCmd.GoToControl "Combo496" Else DoCmd.GoToControl "frame111"
        Case 27 'Escape key
            Rem
        Case 34 'Page Down
            Rem
        Case 37 'Left Arrow
            Rem
        Case 38 'Up arrow pressed
            Rem
        Case 39 'Right Arrow
            Rem
        Case 40 'Down Arrow
            Rem
        Case 46 'Delete Key
            Rem
        Case 48 To 57  'Numbers 0-9
            Rem
        Case 96 To 105 'Numbers 0-9 from the number pad
            Rem
        Case Else
            Rem unallowable character or pass through depending on your situation.
            Rem If an unallowable character add whatever code you need here.
            Rem use docmd.cancel event here to disable certain keystrokes. Such as alpha characters.
    End Select
End Sub
 

Users who are viewing this thread

Back
Top Bottom