You can create a form with nothing on it, and have it open up on Startup (Tools menu, then Startup...). Use the form's Open Event to set it's Visible property to False (i.e. Me.Visible = False) - (you can also use this event to open another form that you may already need to open on Startup). The form will be open but not visible or useable while the database is up. Use the form's Exit Event to execute the code that you wish to run when the database is closed. The user nevers sees or interacts with the form. True, it does reside in memory while the database is open, however, since it has nothing on it, and very little code associated with it, it won't make much difference.
Hope this helps,
JW.