Dont save record if database is closed. (1 Viewer)

Number11

Member
Local time
Today, 12:53
Joined
Jan 29, 2020
Messages
607
I need to stop a record from being saved when the user closes the database and dots click save button on the form

Seems that if you started a data entery and then stopped and just closes the database the record is saved in the able but blank?
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:53
Joined
Sep 21, 2011
Messages
14,223
You need to set appropriate tests in the BeforeUpdate event to see if the record is valid to be saved.
Basic form validation?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:53
Joined
Feb 19, 2002
Messages
43,213
YOU are in complete control over whether or not a record gets saved. As Gasman said, you just need to use the correct event - the form's BeforeUpdate event to cancel the save if the record is not valid. The BeforeUpdate event is like the flapper on a funnel. If the flapper is open, the record gets saved. If the flapper is closed, it doesn't . The BeforeUpdate event is th LAST event to run before a record is saved and there is no way to bypass this event so just put your validation code here and you will be in control:)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:53
Joined
Feb 28, 2001
Messages
27,131
There is more to it than that, though. In the specific case of triggering an update by closing the DB, if all you do is stop the update (in the BeforeUpdate event), then you ALSO stop the DB from closing.

Therefore, you will have to determine WHY the BeforeUpdate event fired, since it will also fire on legit updates. This usually leads to a can of worms, something that you just opened with this problem. It IS soluble, but will take some time to design. You have to start by recognizing that the user's desire to close the DB is inconsistent with YOUR desire to protect the database content and then plan a strategy of what you want to do when this conflict of interests is uncovered.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:53
Joined
Feb 19, 2002
Messages
43,213
WHY the BeforeUpdate event fired
The form's BeforeUpdate event fires ONLY because the current record is dirty and something has made Access want to save the record like closing the form, closing the database, moving from the main form to the subform or vice versa, etc.

A strategy is always required for ensuring data integrity. That strategy requires validation code in the correct event.
 

Users who are viewing this thread

Top Bottom