Closing DAO Objects

We should make a few remarks about closing DAO objects that have been opened programmatically. The Database, Recordset, and Workspace objects each have a Close method. This method will remove these objects from their respective collections. This is appropriate for the three object types mentioned previously for the following reasons:

  • The Databases collection is defined to be the collection of all open database objects.

  • The Recordset objects are temporary objects, to be used only for data manipulation purposes.

  • Attempts to close the default Workspace object are ignored, but you can close other Workspace objects.

Note that objects of types other than the three mentioned are intended to be persistent members of their collections, stored on disk in the Access mdb file. However, they can be removed from their respective collections by using the Delete method.

Here are some caveats to keep in mind with respect to closing objects:

  • As we will see in Chapter 16, you should update (i.e., complete) all pending edits before closing an open Recordset object.

  • When a procedure that declares a Recordset or Database object is exited, the recordset or database is closed and any unsaved changes or pending edits are lost.

  • If you close a Database object while any Recordset objects are still open, or if you close a Workspace object while any of its Database objects are open, those Recordset objects will be automatically closed and any pending updates or edits will be lost.

Get Access Database Design and Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.