Rollback Save Operation...

B

Brett Davis

Hello...

I am frustrated... I have a form that I am using to enter in client
information. If for some reason that the person entering in the data clicks
"close" the record is stilled saved regardless if the person clicked the
"save" button or not. The data is being saved into the Client table which
has an auto-identity field. So when this senario happens the data is saved
and the auto id is incremented.

What I want it to do is... if the user clicks the "close" button before
saving the record I DO NOT want the record to be saved and thereby not
incrementing the auto-id field. Is there some VBA code that I can put
behind my buttons to do this rollback before the record is saved or closed
out?

I am using Microsoft Access 2000

Please advise...

Thank you,

Brett
 
G

Guest

In the Click event of the button you want to close the form:

If Me.Dirty Then
If MsgBox("Do you Want To Save the Current Record before Closing",
vbQuestion + vbYesNo, "Current Record Has Not been Saved") =
vbNo Then
Me.Undo
End If
End If

Also, don't get emotionally involved with autonumbers. Their values are
useless except for use as foreign keys in related tables. I should not
matter what the value is.
 
A

Allen Browne

This won't work if the user closes the form with the built-in close button
(right end of title bar), or does anything that triggers an implicit save
(closes Access, moves record, applies a filter, ...)

The only way to catch every possible thing that could trigger a save is to
use the BeforeUpdate event of the form. Example:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Save?", vbYesNo) <> vbYes Then
Cancel = True
'Me.Undo
End If
End Sub

Now you can avoid that message in the click of your command button if you
declare a module level variable (top of the form's module):
Dim mbAllowSave As Boolean
and set it to True in your button's click event (and reset it):
mbAllowSave = True
RunCommand acCmdSaveRecord
mbAllowSave =False
and test the variable in Form_BeforeUpdate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top