How to close form without saving data

J

Jessica

I am new to Access and would like a simple way clearly explained on how I can
code the close button on my form to close with out saving the data that I
entered(not updated). ie I am adding a new record and realize that I don't
want to add this record to my database so I want to click the close button on
my form and not have it save the data that I just put on the form? Please
help? Thanks Jessica
 
A

Albert D. Kallal

The normal way is to hit the esc key to undo changes (or, use the menu and
go edit->undo).

If you **really** want to place a button, then you can use the following
code. However, it likely better to teach/train the users to use the ESC key,
or the edit->undo, since that will work for ALL forms. Otherwise, for
virtually for EVERY form and application you make, you have to add a button
called:

Exit without Save.

and, if they use an access application written by someone else, they not
have that button, and once again, you would have been better off to teach
the users about edit->undo....

Anyway, here is the code that you can place behind a button that will Exit
without save
(thus, it will work for existing records, and new ones ).

If IsNull(Me!ID) = False Then
If Me.Dirty = True Then
Me.Undo
End If
End If

DoCmd.Close

note: replace !ID with the name of the primary key used for the the forms
recordsource (table)

Lets just hope users don't start hitting that button...and you be back here
in a week asking why so many users are complaining that the record did
not get saved/added...
 
A

Allen Browne

1. Set the command button's On Click property to:
[Event Procedure]

2. Click the Build button (...) beside this.
Access opens the code window.

3. Set up the code like this:
Private Sub Command1_Click()
If Me.Dirty Then Me.Undo
DoCmd.Close acForm, Me.Name
End Sub

That will work unless Access has already saved the record before you click
the button. There are many things that could cause this to happen, e.g.
tabbing through the last control on the form (so it moves to the next
record.)
 

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