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

Last reviewed: December 2, 1997
Article ID: Q113547
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Advanced: 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 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 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.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 FmsHowTo FmsEvnt PgmPrcs
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.