HandsOn Writing the Form KeyDown Event Procedure

1. Open the Suppliers form in Design view.

2. Click the Properties button and, in the Form properties sheet, click the Event tab.

3. Set the Key Preview property to Yes.

4. Save the Suppliers form.

5. In the Code window there are a couple of event procedures already written for this form. Enter the following Form_KeyDown event procedure below the last procedure code.

Private Sub Form_KeyDown _

(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyFl

Private Sub Form_KeyDown _

(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyFl

MsgBox "You

pressed

the

F1 key."

Case vbKeyHome

MsgBox "You

pressed

the

Home key

Case vbKeyTab

MsgBox "You

pressed

the

Tab key.

End Select Select Case Shift

End Select Select Case Shift

Case acShiftMask

MsgBox "You

pressed

the

SHIFT key

Case acCtrlMask

MsgBox "You

pressed

the

CTRL key.1

Case acAltMask

MsgBox "You

pressed

the

ALT key."

End Select End Sub

6. To test the above event procedure, open the Suppliers form in Form view. Press one of the following keys: F1, Home, Tab, Shift, Ctrl, or Alt.

7. Close the Suppliers form.

0 0

Post a comment