Cancel data entry of a record (1 Viewer)

ponneri

Registered User.
Local time
Tomorrow, 02:11
Joined
Jul 8, 2008
Messages
102
Hi.

How can I clear all the contents entered during data entry fields on a form, after say I enter 8 of the 15 fields on a form ?

What I mean is, I dont want that record to be entered. Access defaults to filling a record partly in the underlying table, as I am using a bound form.

Is there a VBA code to do it ??

My form contains mostly text boxes, drop down list, combo boxes and one check box too.

Please help.
 

sneuberg

AWF VIP
Local time
Today, 13:41
Joined
Oct 17, 2014
Messages
3,506
Maybe a button with

Code:
Me.Undo

might do want you want,
 

Minty

AWF VIP
Local time
Today, 21:41
Joined
Jul 26, 2013
Messages
10,366
You can use the forms Before_Update event.

This fires before the record is committed - so you can simply ask via a message box "Save Record?" If the response is no then
Cancel = True
Me.Undo
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:41
Joined
May 7, 2009
Messages
19,231
If you move to another forn while adding record, it will be saved unless you code BeforeUpdate of the form to not to allow partial data entered.
 

ponneri

Registered User.
Local time
Tomorrow, 02:11
Joined
Jul 8, 2008
Messages
102
Thank you all very much.

I did try the me.undo attached to a cancel button on the form itself; but it does not work .

Prompts with a 'cannot undo' message.
( Is the me.undo meant for the last committed record to be rolled back - just for my knowledge)

BTW, I did not code Cancel = True before the me.undo in the code for the cancel button ?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:41
Joined
May 7, 2009
Messages
19,231
Use forms before update event:

Private Sub BeforeUpdate(Cancel As Integer)
If Msgbox("Save record? ", vbYesNo) =vbNo then
Cancel=True
End If
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:41
Joined
Feb 19, 2002
Messages
43,213
If you want to control whether or not a record gets saved, you must use the BeforeUpdate event of the Form. You might have some success if you put code into several other events but you won't intercept ALL saves. Remember Access takes it upon itself to save a record whenever focus moves off the dirty record regardless of what caused the move.

That means that the majority of your validation code belongs in the Form's BeforeUpdate event. There are reasons for putting some code into other events but you will then have to duplicate the code in the BeforeUpdate event so you may as well just put it there.

We've seen nothing of your code and I'm guessing that either you are doing NO validation or your validation is in random other events.

If in addition to cancelling the update, you also want to undo all the changes add:

Me.Undoo

to the suggested code.

Personally I find undo to be punitive and so use it in only two cases.
1. The user is not authorized to change the data at all.
2. I have asked the user if he wants to save the changes and he has said no. I also rarely ask this question. When you bombard users with a lot of popups, they reflexively click OK or Yes without even reading so make sure that OK or Yes doesn't do any damage.
 

Users who are viewing this thread

Top Bottom