G
Garret
Hello,
I have the following code in my forms Before Update event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'If the Record is new
If Me.NewRecord = True Then
ans = MsgBox("You have created a new Record. Do you want to
save?", vbYesNoCancel + vbQuestion, "Save New Record?")
Else
'if the Record is not new
ans = MsgBox("You have changed a Record. Do you want to
save?", vbYesNoCancel + vbQuestion, "Save Record?")
End If
If ans = 7 Then 'if user selects 'No'
Me.Undo 'undo the changes
ElseIf ans = 2 Then 'if user selects
'Cancel'
Cancel = True 'don't undo, don't save
End If
End Sub
This code makes a message box come up if a record has been changed and
the user tries to move to a different record (rather than automatically
save, like Access defaultly does).
However, the same event (BeforeUpdate) is triggered if the user tries
to close out of a form while the record is dirty. 'Yes' saves the
record and then closes the form. 'No' doesn't save the record and
closes the form. (good so far). However, 'Cancel' gives the message:
You can't save this record at this time.
[Database Name] may have encountered an error while trying to save a
record.
If you close this object now, the data changes you made will be lost.
Do you want to close the database object anyway?
Where 'Yes' doesn't save and closes the form, and 'No' just goes back
to the form as if nothing had ever happened. What I want is to have,
when the user presses 'Cancel', exactly the 'No' effect.
In any other application, if you try to close out while your data is
dirty, it prompts this same message box, but when you press Cancel it
acts as though nothing had ever happened. So why does this not work
for mine? And more importantly, how can I make it so?
I have the following code in my forms Before Update event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'If the Record is new
If Me.NewRecord = True Then
ans = MsgBox("You have created a new Record. Do you want to
save?", vbYesNoCancel + vbQuestion, "Save New Record?")
Else
'if the Record is not new
ans = MsgBox("You have changed a Record. Do you want to
save?", vbYesNoCancel + vbQuestion, "Save Record?")
End If
If ans = 7 Then 'if user selects 'No'
Me.Undo 'undo the changes
ElseIf ans = 2 Then 'if user selects
'Cancel'
Cancel = True 'don't undo, don't save
End If
End Sub
This code makes a message box come up if a record has been changed and
the user tries to move to a different record (rather than automatically
save, like Access defaultly does).
However, the same event (BeforeUpdate) is triggered if the user tries
to close out of a form while the record is dirty. 'Yes' saves the
record and then closes the form. 'No' doesn't save the record and
closes the form. (good so far). However, 'Cancel' gives the message:
You can't save this record at this time.
[Database Name] may have encountered an error while trying to save a
record.
If you close this object now, the data changes you made will be lost.
Do you want to close the database object anyway?
Where 'Yes' doesn't save and closes the form, and 'No' just goes back
to the form as if nothing had ever happened. What I want is to have,
when the user presses 'Cancel', exactly the 'No' effect.
In any other application, if you try to close out while your data is
dirty, it prompts this same message box, but when you press Cancel it
acts as though nothing had ever happened. So why does this not work
for mine? And more importantly, how can I make it so?