I've also struggled with this problem. I have used a similar solution as
suggested in the thread below by using a Form_Error event (code below). Yet I
still have a problem with this approach. If a user cancels the Close form
event, this approach seems to cause their unsaved edits thus far to be wiped
clean and a blank form is presented.
I would prefer to keep the record dirty so that the user can complete their
edits. Can anyone Help with this?
Thanks
__________
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate
'Prompt to ask whether to save changes before leaving record
intMsgResult = MsgBox("Do you want to save changes to this comment?",
vbYesNoCancel, "QA Application")
'No - Close without saving changes
If (intMsgResult = vbNo) Then
Me.Undo
GoTo Exit_Form_BeforeUpdate 'To skip date modification
'Cancel - Cancel Form Close and return to the current record.
ElseIf (intMsgResult = vbCancel) Then
'Restore unsaved edits
'Attempted to save dirty field to a variable - Does not work
Me!tbxCommentText = edtCommentText
'Cancel Close event and Flag variable for the Form_Unload &
Form_Error event handlers
Cancel = True
blnCancelUpdate = True
'Skip date modification
GoTo Exit_Form_BeforeUpdate
'Yes - Test for Null values in required fields & Update Last Modified
time stamp
ElseIf (intMsgResult = vbYes) Then
If cbxCategory = Null Or cbxSection = Null Or tbxCommentText =
Null Then
MsgBox "You can not save this Recurring comment" & vbCrLf &
"because a required field is blank."
Cancel = True
blnCancelUpdate = True
End If
End If
'Update Modification Date
Me![LAST_MODIFIED] = Now
Exit_Form_BeforeUpdate:
Exit Sub
Err_Form_BeforeUpdate:
MsgBox Err.Number & " - " & Err.Description _
& vbCrLf & vbCrLf & "Error prompted by: Err_Form_BeforeUpdate"
Resume Exit_Form_BeforeUpdate
End Sub
__________
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
'Test if Cancel was selected on Form_BeforeUpdate event
If blnCancelUpdate = True Then
Cancel = True
Exit_Form_Unload:
Exit Sub
Err_Form_Unload:
MsgBox Err.Number & " - " & Err.Description _
& vbCrLf & vbCrLf & "Error prompted by: Err_Form_Unload"
Resume Exit_Form_Unload
End Sub
__________
Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo Err_Form_Error
'Trap Access save record error due to Dirty record caused by Cancel on
Form_BeforeUpdate event
Const cst_lngCannotSaveRecordErrNo As Long = 2169
'Test that Err.Number = 2169 & Close Event was cancelled by user
If DataErr = cst_lngCannotSaveRecordErrNo And blnCancelUpdate = True Then
Response = acDataErrContinue
End If
Exit_Form_Error:
Exit Sub
Err_Form_Error:
MsgBox Err.Number & " - " & Err.Description _
& vbCrLf & vbCrLf & "Error prompted by: Err_Form_Error"
Resume Exit_Form_Error
End Sub
__________________________________________________________
Dirk Goldgar said:
That explains why I couldn't find the start of the thread.
If the attempt to save the record is forced by the closing of the form,
then Graham's right: the only way to avoid the error message is to trap
the error in the form's Error event. On the other hand, if the attempt
to save the form is caused by some other action -- a move to a new
record, or an explicit request to save the record, then adding "Me.Undo"
to the code in the BeforeUpdate event should prevent it:
If Me.NewRecord Then
If MsgBox( _
"Do you want to save this new document?", _
vbYesNo) _
= vbNo _
Then
Cancel = True
Me.Undo
MsgBox "Update was cancelled", vbOKOnly
End If
End If
If you give the user a "Close" or "Save" button to use instead of just
hitting the little "X" button in the title bar, then you can reduce the
chance of the user's seeing the ugly error message.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)