ACC2: How to Disable PAGE UP and PAGE DOWN Keys in a Form

ID: Q114506


The information in this article applies to:


SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes the following two methods that you can use to disable the PAGE UP and PAGE DOWN keys in a form:


MORE INFORMATION

Method 1: Opening the Form as Dialog Using the OpenForm Macro Action

The PAGE UP and PAGE DOWN keys will be inoperative in a form if the form is opened using an OpenForm macro action with the Window Mode argument set to Dialog. To demonstrate this technique, create and run a new macro with the following actions in the sample database NWIND.MDB.

   OpenForm

      Form Name: Customers
      Filter Name: <leave empty>
      Where Condition: <leave empty>
      View: Form
      Data Mode: Edit
      Window Mode: Dialog 

The drawback to this technique is that a Dialog form cannot use a custom menu, nor will you be able to switch to another form while the Dialog form is open.

NOTE: The Microsoft Access Wizards use this technique to limit navigation in multiple-page forms.

Method 2: Trapping PAGE UP and PAGE DOWN in the KeyDown Event

The PAGE UP and PAGE DOWN keys can be detected and trapped in a control by adding the following lines of Access Basic code to the KeyDown event for all the controls on a form where the PAGE UP and PAGE DOWN keys should be disabled:

   If KeyCode = KEY_PRIOR Or KeyCode = KEY_NEXT Then

      KeyCode = 0

   End If 

NOTE: The constants for KeyCode are defined in the CONSTANT.TXT file, which is included with Microsoft Access. To use these constants in your code, you must either declare them yourself or copy them from the CONSTANT.TXT file and paste them into the Declarations section of your module.

The following example demonstrates how to detect and trap the PAGE UP and PAGE DOWN keys in a control's KeyDown event.

CAUTION: Following the steps in this example will modify the sample database NWIND.MDB. You may want to back up the NWIND.MDB file, or perform these steps on a copy of the NWIND database.
  1. Open the Customers form in Design view.


  2. Create an event procedure with the following code for the KeyDown event for each control on the form:
    
          If KeyCode = KEY_PRIOR Or KeyCode = KEY_NEXT Then
             KeyCode = 0
          End If 

    NOTE: To create each event procedure, select a control, click the Build button to the right of the OnKeyDown property, and then select Code Builder.

    For example, the event procedure code for the Customer ID field will look as follows:
    
          Sub Customer_ID_KeyDown (KeyCode As Integer, Shift As Integer)
             If KeyCode = KEY_PRIOR Or KeyCode = KEY_NEXT Then
                KeyCode = 0
             End If
          End Sub 


  3. Close the module and then switch the form to Form view.


NOTE: Microsoft Access for Windows 95 introduces the new KeyPreview property that enables you to easily trap and disable keys in the form level key events. If the KeyPreview property is set to True, the following code will disable the ESC, PAGE UP, PAGE DOWN keys for the entire form:

     Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
          Select Case KeyCode
               Case 27, 34, 33
               KeyCode = 0
          End Select
     End Sub 


REFERENCES

For more information about disabling PAGE UP and PAGE DOWN in Microsoft Access 95 and 97, please see the following article in the Microsoft Knowledge Base:

Q132031 ACC: How to Disable PAGE UP and PAGE DOWN Keys in a Form (95/97)

For more information about Dialog forms, search for "OpenForm," and then "OpenForm Action" using the Microsoft Access Help menu.

For more information about the KeyDown event, search for "KeyDown," and then "KeyDown, KeyUp Events" using the Microsoft Access Help menu.

Additional query words: pgup pgdn


Keywords          : kbusage FmsOthr 
Version           : 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 3, 1999