Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Modify Keystroke Behavior in Form Objects


View products that this article applies to.

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).

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


Keywords: KB210401, kbusage, kbhowto

↑ Back to the top

Article Info
Article ID : 210401
Revision : 4
Created on : 7/14/2004
Published on : 7/14/2004
Exists online : False
Views : 337