@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:
- Private Sub Form_Unload(Cancel As Integer)
-
Cancel = Not z_blnAllowClose
-
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.