How prevent form closing when pending error?

M

mscertified

My form has lots of valudations that must pass before a record can be saved.
These are coded in BeforeUpdate event. and Cancel is set for errors.
This works great when moving record to record but when the user presses the
exit button or clicks the red X in the top right corner, the form still
closes.

One of the validations is a check for referential integrity, if this check
fails but the form closes, I get the Access "Record cannot be saved" message.
How can I avoid this? I tried putting a cancel in the Form Unload event but
it does not trigger. Here is my unload event:

If (Not Me.NewRecord) And Me.Dirty Then
If Not ValidateForm() Then 'Redo form validations
Cancel = True
End If
End If
 
M

Maurice

Maybe easy way out programming but why not loose the X and create your own
close button so you can keep control of what you are trying to accomplish?
 
L

Linq Adams via AccessMonster.com

If your "exit" button was created by the wizard it uses

DoCmd.Close

which is noted for this behavior. The workaround is to force a save before
closing the form.

If Me.Dirty Then Me.Dirty = False
DoCmd.Close

I have no idea why you're having this problem when closing by using the right
hand corner X. This normally forces a save and hence forces validation code
to be run.
 

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