views:

42

answers:

2

In Access 2003, there are ways of running code when a form or database is opened, but what about when the database is closed?

My motivation is the unavoidable use of a somewhat buggy third-party COM library. Releasing the COM reference (by setting the variable to Nothing) causes it (correctly) to disconnect from its server. The trouble is it can't then re-connect without exiting the process and starting a new one (which is a known bug). In this case, the process is the whole Access IDE :(

Ideally I'd like to store the COM reference somewhere that would be protected from the VBA "Reset" action which clears global variables (and is common during debugging, sometimes forced by a code edit). But then I would like to have the chance to clean up before the database is closed.

A: 

don't release the reference. Let Access do that when it closes.

Beth
But I want to do some cleanup just *before* Access (or the DB) closes. Access won't do it for me.
Hugh Allen
you mean cleanup using your buggy COM library? don't do it within an Access session, wait until they quit Access.
Beth
I'm calling the COM library from Access VBA. I want to do the clean up the same way.
Hugh Allen
I would never want to depend on VBA to release external references. I'd want to clear them all myself when my app exits. The usual approach is to have a hidden form whose OnClose event calls the teardown code.
David-W-Fenton
+2  A: 

You could run code at database close if you have a form which you set to automatically open at database startup ... and leave the form open. Then you can use the form's On Close event to run your cleanup code:

Private Sub Form_Close()
    'do your stuff here '
End Sub
HansUp
+1 Good idea. Now how do I call AddRef from VBA (it gives an error), and cast the COM reference to an integer in order to store it?
Hugh Allen
Never mind, I've posted a whole new question: http://stackoverflow.com/questions/3836046/access-hidden-form-method-to-store-a-com-reference
Hugh Allen
I looked at the other question. Unfortunately I'm clueless.
HansUp