Close Method Example (MDB)

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. The procedure uses the Close method on the Recordset object variable to free the memory resources it has been using. It uses the Set statement with the Nothing keyword to free resources used by the Database object variable.

You can also use the Close method of the Database object to close it and free memory. The Close method of the Database object doesn't actually close the database that's open in Microsoft Access; it only frees the resources used by the Database object variable.

Using an object's Close method and setting the object variable to Nothing are equivalent ways to free memory.

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

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Create table-type recordset.
    Set rst = dbs.OpenRecordset("Customers")
    .
    .
    .
    ' Close recordset to free memory.
    rst.Close                    
    ' Free memory used by object variable.
    Set dbs = Nothing            
End Sub