Can I run a function as access db is closed?

K

Kathy

I want to use FileCopy in VBA to copy the mdb files that contain the back-end
data for my database whenever I close the database or the Access program so
that I have a backup. How can I do that?
 
W

Wayne Morgan

You could have a hidden form that you keep open the entire time the data
base is open. In the Close event of that form (this event will fire when the
form gets closed as part of closing the database) make sure all other open
forms, reports, or anything that would have a connection to the back-end
file(s) are closed, then do the FileCopy.

Be aware that if someone else is still using the back-end, you can probably
copy the file, but you run the risk of the copy being corrupted. It will
probably work 99% of the time, but if someone tries writing to the back-end
just as you try to copy it, you may not get a clean copy.
 
K

Kathy

Thanks, Wayne. I'll try that. I guess I was hoping there was some way I could
just run code when the app was closed with something like an OnCloseApp
method. I'll use your suggestion instead.
 
B

Brossyg

Wayne,

This may work if the command does not rely on data in the form that is being
closed.

I am trying to do something quite similar: When the DB closes, I trigger a
Query in the "OnClose" event of a form that is always open, which records the
time the DB was closed and the ID of the user that closed it. The user's ID
is in a control on the form being closed.

If I close the form with the black X on the FORM, it works perfectly.
However, if I close the whole DB by clicking the ACCESS black X (closing the
whole DB at once), the query cannot find the data in the control in the
form...as if the form is already closed.

When Access is closed, the OnClose action of the forms runs, but the data on
the form isn't available.

Any solutions?

Brossyg
 
D

Dirk Goldgar

In
Brossyg said:
If I close the form with the black X on the FORM, it works perfectly.
However, if I close the whole DB by clicking the ACCESS black X
(closing the whole DB at once), the query cannot find the data in the
control in the form...as if the form is already closed.

When Access is closed, the OnClose action of the forms runs, but the
data on the form isn't available.

Any solutions?

How about using the Unload event instead of the Close event? Does that
make a difference?
 
B

Brossyg

Dirk,

Good suggestion! Using the OnUnload action, the data was available.

Thanks
 

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