ADO Close Method

Home | About me | EXCEL VB Programming (XL97-2003) | ACCESS Programming | EXCEL VB.Net Programming | EXCEL Spreadsheet Functions Material Management  |  Guestbook
 

ADO Close Method

Closes an open object and any dependent objects.

object.Close

Applies To:
Connection Object (ADO)
Record Object (ADO)
Recordset Object (ADO)
Stream Object (ADO)

Remarks:
Use the Close method to close a Connection, a Record, a Recordset, or a Stream object to free any associated system resources. Closing an object does not remove it from memory; you can change its property settings and open it again later. To completely eliminate an object from memory, close the object and then set the object variable to Nothing (in Visual Basic).

Example:

' use the Open and Close methods on both Recordset and Connection objects that have been opened
Public Sub ADO_OpenMethod()
    On Error GoTo ErrorHandler

    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim strCnn As String
    Dim strSQL As String
    Dim varDate As Variant
    
    ' Open connection
    strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=D:\MyData\Northwind.mdb"
    Set cnn = New ADODB.Connection
    cnn.Open strCnn
    
    ' Open employee table
    Set rst = New ADODB.Recordset
    strSQL = "employee"
    rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic, adCmdTable
    
    ' Assign the first employee record's hire date to a variable, then change the hire date
    varDate = rst!hire_date
    Debug.Print "Original data"
    Debug.Print "  Name - Hire Date"
    Debug.Print "  " & rst!fname & " " & _
        rst!lname & " - " & rst!hire_date
    rst!hire_date = #1/1/1900#
    rst.Update
    Debug.Print "Changed data"
    Debug.Print "  Name - Hire Date"
    Debug.Print "  " & rst!fname & " " & _
        rst!lname & " - " & rst!hire_date
    
    ' Requery Recordset and reset the hire date
    rst.Requery
    rst!hire_date = varDate
    rst.Update
    Debug.Print "Data after reset"
    Debug.Print "  Name - Hire Date"
    Debug.Print "  " & rst!fname & " " & _
       rst!lname & " - " & rst!hire_date

    ' clean up
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
    
ErrorHandler:
    ' clean up
    If Not rst Is Nothing Then
        If rst.State = adStateOpen Then rst.Close
    End If
    Set rst = Nothing
    
    If Not cnn Is Nothing Then
        If cnn.State = adStateOpen Then cnn.Close
    End If
    Set cnn = Nothing
    
    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Sub