I'm trying to have an unload function that makes sure the user wishes to leave the form before it automatically resets the information in case they accidentally hit the exit button. But every time they hit the button it resets the form BEFORE it displays the message. Here's the code I have in the form. mSaved is boolean function i made for the entire form so that the only way to save the record is to hit a button.
Private Sub Form_Unload(Cancel As Integer)
If mSaved = False Then
Dim myReply
myReply = MsgBox("Are you sure you wish to exit?", vbYesNo)
If myReply = vbNo Then
DoCmd.CancelEvent
Exit Sub
If myReply = vbYes Then
DoCmd.Close
Exit Sub
Else
mSaved = True
Exit Sub
End If
End If
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If mSaved = False Then
Cancel = True
Me.Undo
Cancel = False
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If mSaved = False Then
Dim myReply
myReply = MsgBox("Are you sure you wish to exit?", vbYesNo)
If myReply = vbNo Then
DoCmd.CancelEvent
Exit Sub
If myReply = vbYes Then
DoCmd.Close
Exit Sub
Else
mSaved = True
Exit Sub
End If
End If
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If mSaved = False Then
Cancel = True
Me.Undo
Cancel = False
End If
End Sub