By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

How to prevent saving data on form in access 2003 when user clicks close button?

P: 23
Can any one tell me----
how to prevent saving data on form in access 2002 or 2003 when user clicks close button of the window or during abnormal shutdown ?
Dec 22 '13 #1
Share this Question
Share on Google+
2 Replies

Narender Sagar
P: 189
Select any important field of the table (on which form is linked) and set 'Required' property to 'Yes'.
Access will not save record until value in that field is entered.
Hope this will serve your purpose.
Dec 22 '13 #2

Expert Mod 5K+
P: 5,397
@Narender: It's not that simplistic. If you will read the OP this is about both the close AND abnormal shutdowns, worse, OP has failed to tell us anything about the design of the form.

@MoonRB: You need to provide much more detail about the form design, if you are concerned about new records, editing existing records, or both.

Narender Sagar does have a point with the required field, however, this is not fool proof and in the correct situations can still have some goofyness happen.

Depending on the form design the edits may or may not have happended, only bound forms and controls have the me.dirty property available so to provide more information here will take a little more detail; however, in general it will involve the before update event of the form and setting cancel to true and hopfully the me.undo is available.

Abnormal shutdowns are very difficult to deal with and you will need to describe what is happening.

To prevent your form from closing by using either the form close button and/or usually the program:

In the declarations section of the form place a private boolean z_blnAllowClose

In in on load event of the form set the private boolean z_blnAllowClose to false

In the on click event of your close command button set the boolean z_blnAllowClose to true

In the unload event of the form, if the boolean z_blnAllowClose is true then set cancel to false (default) if the boolean z_blnAllowClose is false then set the cancel to true, I use the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     Cancel = Not z_blnAllowClose
  3. End Sub
It's a little counter intuitive. Cancel = true means to stop the unload event from happening.

In the before update event I would check the z_blnAllowClose boolean for a true state and set the cancel for the event to true and attempt the me.undo which will stop most dirty record saves. Be careful with the code here in that you don't want to interfere with validation code etc...

Attempt this and let us know what happens, show us the work you've done so that we can help you tweek the code.
Dec 23 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.