Iain:
You might find the following helps. It’s the code for the module of a simple
form with Save and Undo buttons, in which changes to data in the current
record can only be saved, with user confirmation, via the Save button:
''''module starts''''
' updates can only be saved via command button
Option Compare Database
Option Explicit
Dim blnSaved As Boolean
Private Sub cmdSave_Click()
Const MESSAGETEXT = "Save record?"
If Me.Dirty Then
' if user confirms set variable to True and attempt to save record
If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Confirm") = vbYes Then
blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
' if record cannot be saved set variable to False
If Err <> 0 Then
blnSaved = False
End If
Else
blnSaved = False
End If
End If
End Sub
Private Sub cmdUndo_Click()
' undo edits
Me.Undo
End Sub
Private Sub Form_AfterUpdate()
' reset variable to False
blnSaved = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
' cancel update if variable is False,
' i.e. save button has not been clicked
If Not blnSaved Then
Cancel = True
End If
End Sub
Private Sub Form_Current()
' reset variable to False
blnSaved = False
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const IS_DIRTY = 2169
' suppress system error message if form
' is closed while record is unsaved,
' NB: changes to current record will be lost
If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If
End Sub
''''module ends''''
If the user close the form while current changes have not been saved, then
those changes are not saved; if the user attempts to move to another record
while current changes have not been saved then they cannot so without saving
or undoing the changes first.
Ken Sheridan
Stafford, England