Conditionally Close a Form

D

dch3

I have code in a 'CLOSE' button that checks to see if the user has added a
new recorded or started to edit an existing record. The code presents the
user with the option to Save changes & close, Discard changes & close or
Cancel closing the form.

I'd like to have the same code execute when the user clicks on the form's
close button. Which FORM event should I use? onClose has no Cancel and
onUnload fails as Access saves the record prior to the even firing.
 
D

Dirk Goldgar

dch3 said:
I have code in a 'CLOSE' button that checks to see if the user has added a
new recorded or started to edit an existing record. The code presents the
user with the option to Save changes & close, Discard changes & close or
Cancel closing the form.

I'd like to have the same code execute when the user clicks on the form's
close button. Which FORM event should I use? onClose has no Cancel and
onUnload fails as Access saves the record prior to the even firing.


I think you'll have to use the form's BeforeUpdate event, and have a hidden
control on your form that your command buttons set to indicate that it's
okay to update, or at least that your own command button was used. That
will not, however, let you detect the clicking of the "X" button when no
modifications to the form's data have been made.
 
D

dch3

If I've started to update a record and click the 'X', shouldn't the form's
_BeforeUpdate event fire?
 
D

Dirk Goldgar

dch3 said:
If I've started to update a record and click the 'X', shouldn't the form's
_BeforeUpdate event fire?


Isn't that what I said? I just added the caveat that if you want to trap
the form's closing and the record *hasn't* been updated, you can't do it in
the BeforeUpdate event. So you need to use some combination of events for
complete handling of the closing of the form, with and without an update.
 
L

Linq Adams via AccessMonster.com

As Dirk said, move your code to the form's BeforeUpdate event; it'll fire
anytime the record has been changed or a new record created, no matter how
the user tries to exit the form/move to another record. This is standard
procedure for this sort of thing.
 
D

dch3

I tried the _BeforeUpdate event before posting yesterday and it didn't work,
I suspect that its how I've written the code - that or there's an issue with
my keyboard not entering the code as it should be.
 
D

Dirk Goldgar

dch3 said:
I tried the _BeforeUpdate event before posting yesterday and it didn't
work,
I suspect that its how I've written the code - that or there's an issue
with
my keyboard not entering the code as it should be.


That's odd. Yes, the BeforeUpdate event should fire if the form is dirty.
If you have an event procedure for it, make sure the form's BeforeUpdate
property is set to "[Event Procedure]".
 

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