>

Close Method

Applies To

Database Object, Dynaset-Type Recordset Object, Recordset Object, Snapshot-Type Recordset Object, Table-Type Recordset Object, Workspace Object.

Description

Closes an open data access object.

Syntax

object.Close

The object placeholder represents the name of an open Database, Recordset, or Workspace object.

Remarks

Closing an open object removes it from the collection to which it's appended. Any attempt to close the default workspace is ignored.

To remove objects from collections other than the Databases, Recordsets, and Workspaces collections, use the Delete method on those collections.

Caution

Use the Update method (if there are pending edits) and the Close method on all open recordset objects before you close a database. If you exit a procedure that declares recordset or Database objects, the database is closed, any unsaved changes are lost, all pending transactions are rolled back, and any pending edits to your data are lost.

If you try to close a Database object while any recordset objects are open, or if you try to close a Workspace object while any Database objects belonging to that specific Workspace are open, those Recordset objects will be closed and any pending updates or edits will be rolled back.

If you try to close a Workspace object while any Database objects belonging to it are open, the operation closes all Database objects belonging to that specific Workspace object which may result in Recordset objects being closed and pending edits lost.

If the Database object is defined outside the scope of the procedure, and you exit the procedure without closing it, the Database object will remain open until explicitly closed or the module in which it is defined is out of scope.

If the Database, Recordset, or Workspace object named by object is already closed when you use Close, a trappable error occurs.

Using the Close method on either the original or duplicate object doesn't affect the other object. For example, using Close on the original Recordset does not close the clone.

See Also

Clone Method, Delete Method, OpenDatabase Method, OpenRecordset Method.

Specifics (Microsoft Access)

If a Visual Basic procedure contains an object variable that represents the database currently open in Microsoft Access, using the Close method on that object will cause the variable to go out of scope. The Close method will not affect the database that is open in the Microsoft Access Database window.

Example

This example opens and then closes a database and a table-type Recordset object for the Customers table in the database.


Dim dbsNorthwind As Database, rstCustomers As Recordset
' Open database.
Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
' Open table.
Set rstCustomers = dbsNorthwind.OpenRecordset("Customers")


...
rstCustomers.Close    ' Close recordset.
dbsNorthwind.Close    ' Close database.
Example (Microsoft Access)

The following example creates a Database object that points to the current database and opens a table-type Recordset object based on a Customers table in the database. It then uses the Close method on these object variables, which causes them to go out of scope and frees the memory resources they have been using.


Sub UseClose()
    Dim dbs As Database, rst As Recordset

    ' Return Database object that represents current database.
    Set dbs = CurrentDb
    ' Create table-type Recordset.
    Set rst = dbs.OpenRecordset("Customers")
    .
    .
    .
    rst.Close                    ' Close recordset.
    dbs.Close                    ' Close database.
End Sub
Example (Microsoft Excel)

This example opens the Customer recordset of the NWINDEX.MDB database, counts how many records are available, and enters the result on Sheet1.

To create the NWINDEX.MDB database, run the Microsoft Excel example for the CreateDatabase method.


Dim db As Database, rs As Recordset
Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB")
Set rs = db.OpenRecordset("Customer")
Set resultsSheet = Sheets("Sheet1")
resultsSheet.Activate
With resultsSheet.Cells(1, 1)
    .Value = "Records in " & rs.Name & " table:"
    .Font.Bold = True
    .EntireColumn.AutoFit
End With
rs.MoveLast
resultsSheet.Cells(1, 2).Value = rs.RecordCount
rs.Close
db.Close