ACC: How to Trap Keystrokes That Leave a Control on a FormLast reviewed: December 2, 1997Article ID: Q113547 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. This article describes 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 the key's action with another action or disable the action. This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual. NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.
MORE INFORMATION
In Microsoft Access 7.0 and 97In Microsoft Access 7.0 and 97, the ability to trap keystrokes has been greatly improved and enhanced. The KeyDown event is all you need to trap the keystrokes; then, you can change the ones that leave the form to a value of zero. This is demonstrated in the following sample Visual Basic for Applications code segment:
Private Sub Text2_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
Note: In Microsft Access 7.0 and Microsoft Access 97 controlling the tab order is much easier.
If you are writing this code to control where the tab key moves next in a form, consider using the TabStop property or the Tab Order menu option.
To control the Tab Order for a form with the menus:
---------------------------------------------------
Open the form in design view.
On the View menu click Tab Order.
To find more information about the TabStop property:
----------------------------------------------------
On the Help menu click Contents and Index.
In the Index tab type "tabstop" without the quotes.
In Microsoft Access 1.x and 2.0The example below demonstrates how to use the GetKeyState() Windows application programming interface (API) call to detect which key or key combination is used to exit a control on a form. This technique can be used to prevent a user from exiting a control, or to remap the key or key combination to a different action: NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.
NOTE: In Microsoft Access 2.0 you can accomplish this by setting the EnterKeyBehavior property of a control to New Line in Field.
|
Additional query words: trapping
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |