Prevent a record being saved after an error occurs

T

True.Kilted.Scot

On my form, I have a "Save" button which is used to save the current
record. I believe that it is not necessary to save the record, as it
will be saved automatically when I move to another record, or create a
new one--perhaps someone can clarify this for me?

Anyway, when the user clicks on the "Save" button, it runs the
"cmdSaveRecord" sub which, amongst other things, executes this command:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

If I trace the execution of the code, it then goes into the
"BeforeUpdate" event of the form. Within here, some validation is
performed on one of the data entry controls. If the control fails
validation, then I alert the user via MsgBox and then execute the code:

Cancel = True

The "BeforeUpdate" event exits, and returns me to the "cmdSaveRecord"
sub. However, once in here, it triggers my error trapping procedure
with the following:

Err.Number = 2501
Err.Description = The DoMenuItem action was cancelled.


Questions:

1. Do I really need the "Save" button, or can I remove it?
2. I realise the DoMenuItem line that I have has been replaced
with "RunCommand".
Should I replace the above line with "RunCommand
acCmdSaveRecord"?
3. As the form's BeforeUpdate has been cancelled, due to an
invalid entry, I obviously
don't want the record to be saved to the database. How do
I achieve this? Would I
be best advised to place the validation within the
"LostFocus" or "OnExit" event of
the desired control? Or, is there some other way of
avhieving what I'm looking for?

Many thanks & regards

Duncs
 
M

Martin

If you've cancelled the updating of the form from the BeforeUpdate event, the
record won't have been saved so that should be enough - you could indeed put
all of your individual controls' validations into the form's BeforeUpdate
event and undo them in the same way.

As to the Save button, you don't really need it (Shift-Enter does the same
thing) but users do like to see it; it means they can save the record without
moving on to another and stay looking at it on screen.
 
B

Baz

On my form, I have a "Save" button which is used to save the current
record. I believe that it is not necessary to save the record, as it
will be saved automatically when I move to another record, or create a
new one--perhaps someone can clarify this for me?

Correct


Anyway, when the user clicks on the "Save" button, it runs the
"cmdSaveRecord" sub which, amongst other things, executes this command:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

If I trace the execution of the code, it then goes into the
"BeforeUpdate" event of the form. Within here, some validation is
performed on one of the data entry controls. If the control fails
validation, then I alert the user via MsgBox and then execute the code:

Cancel = True

The "BeforeUpdate" event exits, and returns me to the "cmdSaveRecord"
sub. However, once in here, it triggers my error trapping procedure
with the following:

Err.Number = 2501
Err.Description = The DoMenuItem action was cancelled.


Questions:

1. Do I really need the "Save" button, or can I remove it?

Having a "Save" button can make the process clearer to users, but if you
don't have one, the record will get saved anyway when the user moves to a
different record or closes the form.

2. I realise the DoMenuItem line that I have has been replaced
with "RunCommand".
Should I replace the above line with "RunCommand
acCmdSaveRecord"?

The most compact code for forcing a record to be saved is thus:

Me.Dirty = False

3. As the form's BeforeUpdate has been cancelled, due to an
invalid entry, I obviously
don't want the record to be saved to the database. How do
I achieve this? Would I
be best advised to place the validation within the
"LostFocus" or "OnExit" event of
the desired control? Or, is there some other way of
avhieving what I'm looking for?

If the BeforeUpdate event is cancelled, the record is NOT saved. You are
doing it right! Probably you want to modify your error trapping so it
doesn't display anything to the user when error 2501 occurs (presumably you
have already displayed a validation error message whilst in the BeforeUpdate
event).
 

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