ACC2000: How to Modify Keystroke Behavior in Form Objects (210401)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210401
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article shows you how to trap the ENTER, TAB, SHIFT+TAB, PAGE UP, and PAGE DOWN keys when they are pressed in a form, and how to either replace a given key's action with another action or disable the action.

MORE INFORMATION

In Microsoft Access 2000, you can use the KeyDown event to trap keystrokes. You can then assign a value of 0 (zero) to the keystrokes that cause the text box or other data object to lose focus. Doing this prevents these keys from being used to move the focus to a different data object.

To disable the TAB, ENTER, PAGE UP, and PAGE DOWN keys, follow these steps:

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access and open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
  2. Open the Employees form in Design view.
  3. Assign the following code to the On KeyDown property of the Extension text box:
    Private Sub Extension_KeyDown(KeyCode As Integer, Shift As Integer)
        Select Case KeyCode
            ' If user presses TAB, ENTER, PAGE UP, PAGE DOWN
            Case 13, 9, 33, 34
                ' Disable the keystroke by setting it to 0
                KeyCode = 0
            Case Else
                Debug.Print KeyCode, Shift
        End Select
    End Sub
    					
  4. Open the Employees form in Form view. Tab through the text boxes and continue to press TAB when the focus moves to the Extension text box.

    Notice that Access does not advance to the next record, and that pressing PAGE UP, PAGE DOWN, SHIFT+TAB, and ENTER has no effect in this text box. However, you can still move to other text boxes by using the mouse pointer or by pressing the UP ARROW key.

Modification Type:MinorLast Reviewed:7/14/2004
Keywords:kbhowto kbusage KB210401