Open, Close Events — Event Procedures Example

The following example shows how you can cancel the opening of a form when the user clicks a No button. A message box prompts the user to enter order details. If the user clicks No, the Order Details form isn't opened.

To try the example, add the following event procedure to a form.

Private Sub Form_Open(Cancel As Integer)
    Dim intReturn As Integer
    intReturn = MsgBox("Enter order details now?", vbYesNo)
    Select Case intReturn
        Case vbYes
            ' Open Order Details form.
            DoCmd.OpenForm "Order Details"
        Case vbNo
            MsgBox "Remember to enter order details by 5 P.M."
            Cancel = True                    ' Cancel Open event.
    End Select
End Sub