formsms-accessms-access-2010keypreview

What is meant by 'key preview' property of a form in MS Access?


I am right now programming in MS Access and while going through the properties of a form I have come across a property named 'key preview'. MS Access documentation tells that it is used to invoke keyboard events for forms before keyboard events for controls.

I am not sure what this means and whay could be possible usage of this property??


Solution

  • If the the KeyPreview property is set to true, the form will receive the keyboard event before the control does, giving you a chance to do something with it at the form level.

    Now, imagine that you have a form with many controls on it.
    Imagine that you wish to allow the user to hit the F2 key on the keyboard to perform some action, like opening another form, playing some music, or whatever.

    In Access, like in most UI programming, only one control can receive focus at any one time. So if the focus is on a textbox, when you hit F2, that textbox keyboard events will fire.
    You can catch that keypress from the textbox events, but if the user hits F2 from another control, it won't be detected unless you also detect that F2 was hit from that control itself.

    So to make things easier, you can just set the form to receive the keyboard events first, before they are passed on to the control that has focus, giving you a chance to detect that the user pressed F2 in a single place, rather than having to wire each control to detect that keypress.

    Example

    Add a single textbox mytextbox to a new form. In the code behind for the form, add the following events for catching keyboard events for both the form and the textbox:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Debug.Print "Form_KeyDown(keycode:" & KeyCode & ", Shift:" & Shift & ")"
    End Sub
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
        Debug.Print "Form_KeyPress(KeyAscii:" & KeyAscii & ")"
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        Debug.Print "Form_KeyUp(keycode:" & KeyCode & ", Shift:" & Shift & ")"
    End Sub
    
    Private Sub mytextbox_KeyDown(KeyCode As Integer, Shift As Integer)
        Debug.Print "mytextbox_KeyDown(keycode:" & KeyCode & ", Shift:" & Shift & ")"
    End Sub
    
    Private Sub mytextbox_KeyPress(KeyAscii As Integer)
        Debug.Print "mytextbox_KeyPress(KeyAscii:" & KeyAscii & ")"
    End Sub
    
    Private Sub mytextbox_KeyUp(KeyCode As Integer, Shift As Integer)
        Debug.Print "mytextbox_KeyUp(keycode:" & KeyCode & ", Shift:" & Shift & ")"
    End Sub
    

    Now, open the form and type a single key.
    If you hit the Q key, you should see something like this in the immediate window:

    Control Keyboard events

    Only the textbox received the events.

    Now, set the KeyPreview property to true (Yes) and when you open the form and hit Q, you should see that the Form received the Keyboard events first.

    Form and Control Keyboard events