Report.KeyPreview property (Access)

Use the KeyPreview property to specify whether the report-level keyboard event procedures are invoked before a control's keyboard event procedures. Read/write Boolean.

Syntax

expression.KeyPreview

expression A variable that represents a Report object.

Remarks

The KeyPreview property uses the following settings.

Setting Visual Basic Description
Yes True The report receives keyboard events first, and then the active control receives keyboard events.
No False (Default) Only the active control receives keyboard events.

You can set the KeyPreview property in any view.

Use the KeyPreview property to create a keyboard-handling procedure for a report. For example, when an application uses function keys, setting the KeyPreview property to True allows you to process keystrokes at the report level rather than writing code for each control that might receive keystroke events.

To handle keyboard events only at the report level and prevent controls from receiving keyboard events, set the KeyAscii argument to 0 in the report's KeyPress event procedure, and set the KeyCode argument to 0 in the report's KeyDown and KeyUp event procedures.

If a report has no visible or enabled controls, it automatically receives all keyboard events.

Example

In the following example, the KeyPreview property is set to True in the report's Load event procedure. This causes the report to receive keyboard events before they are received by any control. The report's KeyDown event then checks the KeyCode argument value to determine if the F2, F3, or F4 keys were pressed.

Private Sub Report_Load() 
 Me.KeyPreview = True 
End Sub 
 
Private Sub Report_KeyDown(KeyCode As Integer, Shift As Integer) 
 Select Case KeyCode 
 Case vbKeyF2 
 ' Process F2 key events. 
 Case vbKeyF3 
 ' Process F3 key events. 
 Case vbKeyF4 
 ' Process F4 key events. 
 Case Else 
 End Select 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.