How to: Prompt a User Before Closing a Form

Access Developer Reference

The following example illustrates how to prompt the user to verify that the form should closed.

  Private Sub Form_Unload(Cancel As Integer)
    If MsgBox("Are you sure that you want to close this form?", vbYesNo) = vbYes Then
        Exit Sub
    Else
        Cancel = True
    End If
End Sub