Code to view save confirm message (1 Viewer)

MAAS8000

Registered User.
Local time
Yesterday, 22:22
Joined
Apr 3, 2018
Messages
38
i have used this code to view a message to confirm saving
the case yes & no working well
but the case cancel i need to make me remain in the same record without saving, but it work like cancel


Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty = True Then
If MsgBox("Do You Want To Save?", vbYesNoCancel + vbInformation, "Save?") = vbNo Then Me.Undo
ElseIf vbCancel Then
Me.Undo
Me.SetFocus
End If
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:22
Joined
Feb 19, 2002
Messages
42,981
Checking for dirty is redundant. This event is only executed when a record is dirty. Also, using the If then else method, you need to repeat the original if statement and that would prompt twice so you need to switch to using a Select Case so you can prompt once but handle three options. And finally, when you want to cancel the save, you must use the Cancel argument otherwise Access will attempt to save the empty record. Undo removes everything typed in the form but doesn't tell Access to not save.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Select Case MsgBox("Do You Want To Save?", vbYesNoCancel + vbInformation, "Save?") 
        Case vbNo 
            Me.Undo
            Cancel = True
            Exit Sub
        Case vbCancel 
            Cancel = True
            Exit Sub
    End Select

'''' the rest of your validation code goes here.

End Sub
 

Users who are viewing this thread

Top Bottom