
Originally Posted by
benjammin
nevermind, i just give the user the option to save record if dirty.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then
Me.Undo
End If
End If
End Sub
The biggest problem with this is it asks the user to save? even if they press the save and add new record button I have, or the save and close button, essentially it's redundant...
What do I do! I only want this question asked when they close the form with alt-f4, x button, ctrl-w, etc...
The solution is to use a flag. Create a boolean flag at the top of the form's module:
Code:
Private blnSave As Boolean
Then, in the click event of the buttons to save or add new record use:
and then in the BeforeUpdate event (The If Me.Dirty is redundant because the BeforeUpdate event will not fire unless the form is dirty):
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not blnSave Then
If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then
Cancel = True
Me.Undo
End If
End Sub
And so then it will not ask them if they want to save if they have clicked the button(s) and it will ask them the question ONLY if they haven't clicked the button.