Hi. You can provide a 'Close without saving changes' button on your form which undoes the current entry if changes have been made, on confirmation that abandoning the current record is what the user wants to do. If the button is named cmdAbandon, say, then the sample code below would provide a specific route out for the user:
- Private Sub cmdAbandon_Click()
-
Dim strMessage As String
-
Dim blChangesMade As Boolean
-
blChangesMade = Me.Dirty
-
strMessage = "Are you sure you want to "
-
If blChangesMade Then
-
strMessage = strMessage & "abandon the changes you have made?"
-
Else
-
strMessage = strMessage & "exit from this form?"
-
End If
-
If MsgBox(strMessage, vbExclamation + vbYesNo, "Abandon this entry") = vbYes Then
-
If blChangesMade Then
-
Me.Undo
-
End If
-
DoCmd.Close
-
End If
-
End Sub
This does not prevent the user from closing the form using the form's top-right close button, which would automatically save any changes made. There are ways round this, but these can be a little more complex to handle, and if you have provided a specific 'abandon' route you should really not need to go for the extra complexity of undoing the update in these circumstances.
-Stewart