This article was previously published under Q210401
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
This article shows you how to trap the ENTER, TAB, SHIFT+TAB, PAGE UP, andPAGE 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.
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.
Start Microsoft Access and open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
Open the Employees form in Design view.
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 SelectEnd Sub
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.