Abort Record Edit / Add

A

Alasdair

Just a quick question that i should know the answer to !

I have a form that I am using to edit / update records
from a table.

I want to add a Cancel button that will close the form
whilst abandoning any changes that have been made to the
record currently being added / edited in the form.

Although I can add a cancel button, the record is still
updated when this is pressed.

Many thanks,

Alasdair
 
A

Allen Browne

Private Sub cmdCancel_Click()
If Me.Dirty Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name
End Sub
 
R

Ronald Dodge

Along with his question, how do you prevent a control level validation check
(I.e. BeforeUpdate Event) from taking place when the user clicks on the
"Cancel" command button? Reason for this question, if the BeforeUpdate
Event is triggered, and the check causes the validation to set the Cancel
to -1 (True), the Cancel command doesn't get executed.
 
G

Guest

Many thanks - I couldn't find the Undo Method !!
-----Original Message-----
Private Sub cmdCancel_Click()
If Me.Dirty Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.




.
 
A

Allen Browne

Right: the control's BeforeUpdate has to fire before focus can move to the
command button and its Click event can run.

Teach the user to press Esc to undo their change instead of relying on a
command button. Or provide a Cancel button on the toolbar instead of the
form.
 
R

Ronald Dodge

That's the very reason why I have created my own custom Validation code to
get around the issue, but unfortunately, this also means that my forms has
to be unbound for this to work.
 
R

Ronald Dodge

Actually, I found unbound forms to be easier to work with cause I don't have
to play as much of the guess work with Access as I would have to do with
bound forms. Not only that, but I can also setup my own error checks and
messages along with the way it's done rather than using Access' generalized
error messages, but then the error checking stuff could be done in the
BeforeUpdate Event anyhow.

The main thing I found with my nearly 5 years of VBA coding experience, the
more of the guess work I take out of the language's coding, the less hassels
I will likely have to deal with down the road. It even gotten to the point
that I had to set up my code as to what, when, where, and how the numbers
gets calculated, which could only be controlled via VBA, due to the various
issues that I ran into including the issues that redundant calculations
created. Pardon me, if some of this sounds more like Excel VBA, but that's
where my VBA experience got started.
 
A

Allen Browne

Okay: each to their own experience, but mine is the opposite. Using
Form_BeforeUpdate for record-level validation is reliable and fast.
 

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