Solved Saving records (1 Viewer)

Dumferling

Member
Local time
Today, 13:15
Joined
Apr 28, 2020
Messages
102
Most of my forms have a save button on them. What I find is that when the user click on the save button and the code executes, Access jumps to the BeforeUpdate event which, as a default, has similar code in it. It jumps when it hits the line in the Save button code which commits the changes to the underlying table. My objective is to ensure that changes are deliberately saved (access will save things when you move off the record but I need my users to make a deliberate choice about the save). So I want the BeforeUpdate event to trigger if there is unsaved data and the user moves to another record but I also want to be able to jump the code in the BeforeUpdate event if the BeforeUpdate event is being triggered by the user clicking the save button. To put it simply:
User clicks Save button - BeforeUpdate event code is bypassed and Save button procedure saves record
User moves to another record without clicking Save button - BeforeUpdate event code runs to check if they want to save the changes

The only way I can think of doing this is by somehow passing a value from the Save procedure to the BeforeUpdate event procedure. Is there a simpler way to do this? And, if not, how can get the BeforeUpdate event to recognise that it is being triggered by someone clicking the save button?

Right now I am feeling that I am being dense and there is simple way to handle this.
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:15
Joined
Sep 21, 2011
Messages
14,304
Set a global variable or TempVar in your Save button code and check for that in the Before_Update event. ?
However bypassing that event is not advisable, that event should check you have whatever you need before saving and everything is correct.
 

GK in the UK

Registered User.
Local time
Today, 12:15
Joined
Dec 20, 2017
Messages
274
Before Update will fire regardless of how the record gets saved. If a record is dirty, you simply cannot stop it from firing other than pulling the plug or killing Access. It’s the last thing that happens before the record actually gets updated to the disk, and it’s where you should have your field validation code. If there’s a problem with the data, you have a line in Before Update, Cancel=true and that’s the only way you can stop a dirty record from saving.

If you have validation code behind your Save button you should move it into Before Update. It sounds as though you have it in two places.

If you save the record with a button and Cancel the save in Before Update the sub that attempted the save will receive an error message and you must trap it in an error handler so it doesn’t appear. Think if it as “hey, you ordered me to save and I tried but it didn’t happen and here’s the error code”. It doesn’t matter that it was your code that cancelled the save the error message still gets sent back. You should have your own user friendly message in Before Update but you should never bypass your validation code.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:15
Joined
Oct 29, 2018
Messages
21,473
The only way I can think of doing this is by somehow passing a value from the Save procedure to the BeforeUpdate event procedure. Is there a simpler way to do this? And, if not, how can get the BeforeUpdate event to recognise that it is being triggered by someone clicking the save button?
You could try checking if the Active Control is the Command Button. Just a thought...
 

Dumferling

Member
Local time
Today, 13:15
Joined
Apr 28, 2020
Messages
102
Thank you for the advice. I used Tempvars (not without a lot of headscratching) so that it will skip saving on BeforeUpdate provided it has saved the record already.
 

Mike Krailo

Well-known member
Local time
Today, 07:15
Joined
Mar 28, 2020
Messages
1,044
ArnelGP answered a similar thread about this sort of thing many years ago. HERE. In that case, he used a form level boolean variable to do the job. Glad you got it working. You can mark your post as solved.
 

Users who are viewing this thread

Top Bottom