Stopping automatic SAVE on close

S

Steve S

Have a form whose sole purpose in life is to ADD new records to a table. I
have a SAVE button on the form that handles various data integrity checking
and the users should use that button to save data. If any or all fields are
dirty and the user closes the form I do not want any data saved.

I have tried putting 'If Me.Dirty Then Me.Undo' in both the forms Close and
Unload events but junk still gets saved.

One solution is to not bind the form to the table and use DAO code to update
the record but there must be a way to just undo updates so junk is not
automatically saved on close

Any and all help is appreciated

steve
 
D

Douglas J. Steele

One approach is to set a module-level variable when the Save button is
clicked:

Private Sub cmdSave _Click()

booOkay = True

End Sub

In the form's BeforeUpdate event, set whether that variable is set, and only
save the record if it is:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If booOkay = False Then
Cancel = True
Me.Undo
End If

End Sub

Don't forget to set the variable back once the record's saved:

Private Sub Form_AfterUpdate

booOkay = False

End Sub
 

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