Close form without saving

C

CW

I have a form where we add new debtor records.
It has a Save button which works fine when a new record has been created. It
saves the record and closes the form.
It also has a Close button which should close the form WITHOUT saving
anything that has been entered, such as when the user decides to abort for
some reason. However, at the moment it does save and create a new record in
the underlying table. How can I prevent this, i.e. get it to do a close and
NOT save?
I am using the code shown below, on the Click event of the Close button.
Many thanks
CW

Private Sub DebtorFormCloseBtn_Click()
On Error GoTo Err_DebtorFormCloseBtn_Click


DoCmd.Close

Exit_DebtorFormCloseBtn_Click:
Exit Sub

Err_DebtorFormCloseBtn_Click:
MsgBox Err.Description
Resume Exit_DebtorFormCloseBtn_Click

End Sub
 
R

Robert_DubYa

It sounds like your "Save Button" is just a confidence builder. Access
stores the information when it is typed into the form.

When your form is "Saved" Does it bring you to a new record? If so you can
just empty all the current fields on the report when it close using VB.
 
S

stumac

Hi CW - access aoutomatically saves the record as you go along - try changing
your code to:

Me.Undo
DoCmd.Close

Hth
Stu
 
C

CW

Robert -
Thanks but you seem to have missed my question. As I said, the "Save"
process is fine. The form saves and closes. No problem.
It is the "Close" process that I want to change. What I want is some code
behind the clicking of the "Close" button that prevents it saving what has
just been entered on the form.
Thanks
CW
 
D

Daniel Pineault

CW,

You are trying to work against Access. As soon as you type a value it is
saved. Your Save button may as well not even exist.

If you truly wish to build a form to go against Access inate behavior to try
and simulate other software, then you'd need to make an unbound form. Then
create a query to retrieve values when searched and build an insert/update
query to perform the save and then you could have a close button as you wish.
A lot of work in my opinion to achieve very little, but you may have a valid
reason for going to all this trouble.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
J

John W. Vinson

CW,

You are trying to work against Access. As soon as you type a value it is
saved. Your Save button may as well not even exist.

That isn't exactly the case, Daniel: if a bound Form has multiple textboxes,
the data is *not* saved as each textbox is typed; instead, it saves the entire
record when the user moves off the record, moves into or off of a subform,
closes the form, presses shift-enter, etc.
If you truly wish to build a form to go against Access inate behavior to try
and simulate other software, then you'd need to make an unbound form. Then
create a query to retrieve values when searched and build an insert/update
query to perform the save and then you could have a close button as you wish.
A lot of work in my opinion to achieve very little, but you may have a valid
reason for going to all this trouble.

Can be useful but as you say, a lot of work for often minor benefit.
 
C

CW

John -
Many thanks for the clarification
CW

John W. Vinson said:
That isn't exactly the case, Daniel: if a bound Form has multiple textboxes,
the data is *not* saved as each textbox is typed; instead, it saves the entire
record when the user moves off the record, moves into or off of a subform,
closes the form, presses shift-enter, etc.


Can be useful but as you say, a lot of work for often minor benefit.
 
C

CW

Ken -
I have been away and unable to try this yet but it looks like exactly what I
need
Many thanks
CW
 
C

CW

Daniel -
Many thanks for the advice. It seems very unhelpful for Access to insist on
saving whilst a record is being edited without giving any opportunity to undo
the data just entered if it turns out to have been incorrect. It's easily
done, it happens in the real world, users open the wrong record and start
typing away, or for whatever reason, sometimes you don't want to save after
all.
Thanks
CW
 
J

John W. Vinson

Many thanks for the advice. It seems very unhelpful for Access to insist on
saving whilst a record is being edited without giving any opportunity to undo
the data just entered if it turns out to have been incorrect. It's easily
done, it happens in the real world, users open the wrong record and start
typing away, or for whatever reason, sometimes you don't want to save after
all.

Access does indeed provide this capability, in several different ways.

Pressing the <Esc> key once will cancel edits to the currently active control.
Pressing it twice will cancel the edit to the currently selected record,
restoring its previous values. This has saved my bacon several times.

A Form also has a BeforeUpdate event which can be cancelled. You can prompt
the user to save or discard changes in this event:

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
iAns = MsgBox("Click OK to save, Cancel to cancel", vbOKCancel)
Cancel = (iAns = vbCancel)
End Sub

though in practice, users will be annoyed ("if I didn't want to save the
record I wouldn't have entered it!!!!") and just blindly click OK.
 

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