I have a database in which I need users to save forms via command buttons only, since I run additional code with those buttons. As such, I've hidden form close buttons.
I've now realized that if they close the entire database with a dirty form open, that will save the form. Oops.
I wrote code that pops up a msgbox in this situation- it allows users to close the database without saving the form, or to return to the form.
I allow users to return to the form by inserting "Cancel=True" into the Before Update event (as well as the On Unload event).
The problem is, when this code runs, all of their changes become undone. Is there any way to allow them to return to the form without saving the changes, but without discarding them either?
Many thanks for your help, good people of computer land.
Matt
... And in case this is helpful at all, here's the full code:
Dim StopClose As Boolean
Dim StopUpdate As Boolean
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
StopClose = False
If StopUpdate = True Then
If Me.Dirty Then 'If form was changed
' Specify the message to display.
strMsg = "Are you sure you want to close without saving?" & Chr(10) & Chr(10)
strMsg = strMsg & "Click YES to Close; or NO to Return to Form."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Close Without Saving?")
' Check the user's response.
If iResponse = vbYes Then
DoCmd.RunCommand acCmdUndo
Else
Cancel = True
StopClose = True
End If
End If
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If StopClose = True Then
Cancel = True
StopClose = False
End If
End Sub