ACC: How to Trap Keystrokes That Leave a Control on a Form

ID: Q113547

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

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 the key's action with another action or to 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 97

In 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 Microsoft 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.0

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

1. Create a module and type the following line in the Declarations section:

      Option Explicit

      Global Const VK_CONTROL = &H11
      Global Const VK_SHIFT = &H10
      Global Const VK_TAB = &H9
      Global Const VK_RETURN = &HD
      Global Const VK_PRIOR = &H21  ' PGUP
      Global Const VK_NEXT = &H22   ' PGDN
      Declare Function GetKeyState% Lib "user.exe" (ByVal nKey%)

2. Type the following procedure:

      Function CheckKey ()
         Dim sControl As String
         Dim sShift As String

         ' Was the CTRL or SHIFT key used?
         If GetKeyState(VK_CONTROL) < 0 Then sControl = "Ctrl+"
         If GetKeyState(VK_SHIFT) < 0 Then sShift = "Shift+"

         ' Was the TAB key used?
         If GetKeyState(VK_TAB) < 0 Then
            MsgBox "You pressed " & sControl & sShift & "TAB!"
         End If

         ' Was the ENTER or RETURN key used?
         If GetKeyState(VK_RETURN) < 0 Then
            MsgBox "You pressed " & sControl & sShift & "ENTER!"
         End If

         ' Was the PAGE DOWN key used?
         If GetKeyState(VK_NEXT) < 0 Then
            MsgBox "You pressed " & sControl & sShift & "PGDN!"
         End If

         ' Was the PAGE UP key used?
         If GetKeyState(VK_PRIOR) < 0 Then
            MsgBox "You pressed " & sControl & sShift & "PGUP!"
         End If

      End Function

3. Open any form in Design view, and set any control's OnExit property to:

      =CheckKey()

4. View the form in Form view, select the control you modified in step
   3, and then try to exit the control.

The next example demonstrates how to use the GetKeyState() Windows API call in Microsoft Access 1.x to modify the ENTER key's action so that it adds lines to a text box, rather than moving to the next control in the tab order.

NOTE: In Microsoft Access 2.0 you can accomplish this by setting the EnterKeyBehavior property of a control to New Line in Field.

1. Create a module and type the following line in the Declarations section:

      Option Explicit
      Global Const VK_RETURN = &HD
      Declare Function GetKeyState% Lib "user.exe" (ByVal nKey%)

2. Type the following procedure:

      Function MakeEnterAddLines()
         If GetKeyState(VK_RETURN) < 0 Then
            DoCmd CancelEvent
            SendKeys "^{ENTER}"
         End If
      End Function

3. Set the OnExit property of any text box control on any form to:

      =MakeEnterAddLines()

4. View the form in Form view, move to the control you modified, and then
   press ENTER.

The next example demonstrates how to use the GetKeyState() Windows API call to disable the PAGE UP and PAGE DOWN keys in a form:

1. Create a module and type the following line in the Declarations section:

      Option Explicit
      Global Const VK_PRIOR = &H21  ' PGUP
      Global Const VK_NEXT = &H22   ' PGDN
      Declare Function GetKeyState% Lib "user.exe" (ByVal nKey%)

2. Type the following procedure:

      Function DisablePGUP_PGDN()
         If GetKeyState(VK_PRIOR) < 0 Or GetKeyState(VK_NEXT) < 0 Then
            DoCmd CancelEvent
         End If
      End Function

3. Set the OnExit properties of all the controls on any form where the PAGE
   UP and PAGE DOWN keys are to be disabled to:

      =DisablePGUP_PGDN()

The next example demonstrates how to use the GetKeyState() Windows API call to cause the TAB key, when it is pressed in the last control in the last record in a subform, to navigate to the next control on the main form:

1. Create a module and type the following line in the Declarations section:

      Option Explicit
      Global Const VK_TAB = &H9
      Global Const VK_SHIFT = &H10
      Declare Function GetKeyState% Lib "user.exe" (ByVal nKey%)

2. Type the following procedure:

      Function TABtoMainForm(F As Form)
         Dim DS As Dynaset
         On Error Goto Bye_TABtoMainForm
         ' Move to last record in form dynaset.
         Set DS = F.Dynaset
         DS.MoveLast
         ' See if the form record is the last record.
         If DS.Bookmark = F.Bookmark Then
            If GetKeyState(VK_TAB) < 0 And Not _
            (GetKeyState(VK_SHIFT) < 0) Then
               DoCmd CancelEvent
            SendKeys "^{TAB}"
         End If
      End If

      Bye_TABtoMainForm:
      End Function

3. Set the OnExit property of the last control in the subform to:

      =TABtoMainForm(Form)

Additional query words: trapping
Keywords          : kbusage FmsEvnt FmsHowto PgmPrcs 
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998