Método Close de conexão, exemplo da propriedade Type de tabela (VB)
Aplica-se ao: Access 2013, Office 2013
A configuração da propriedade ActiveConnection como Nothing deve "fechar" o catálogo. As coleções associadas estarão vazias. Todos os objetos que foram criados com os objetos de esquema no catálogo ficarão órfãos. Todas as propriedades desses objetos que tenham sido armazenadas em cache ainda estarão disponíveis, mas a tentativa de ler propriedades que requeiram uma chamada para o provedor falharão.
' BeginCloseConnectionVB
Sub Main()
On Error GoTo CloseConnectionByNothingError
Dim cnn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
cnn.Open "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source= 'c:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb';"
Set cat.ActiveConnection = cnn
Set tbl = cat.Tables(0)
Debug.Print tbl.Type ' Cache tbl.Type info
Set cat.ActiveConnection = Nothing
Debug.Print tbl.Type ' tbl is orphaned
' Previous line will succeed if this was cached
Debug.Print tbl.Columns(0).DefinedSize
' Previous line will fail if this info has not been cached
'Clean up
cnn.Close
Set cat = Nothing
Set cnn = Nothing
Exit Sub
CloseConnectionByNothingError:
Set cat = 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
' EndCloseConnectionVB
Fechar um objeto Connection que tenha sido usado para "abrir" o catálogo é o mesmo que configurar a propriedade ActiveConnection como Nothing.
Sub CloseConnection()
On Error GoTo CloseConnectionError
Dim cnn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
cnn.Open "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source= 'c:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb';"
Set cat.ActiveConnection = cnn
Set tbl = cat.Tables(0)
Debug.Print tbl.Type ' Cache tbl.Type info
cnn.Close
Debug.Print tbl.Type ' tbl is orphaned
' Previous line will succeed if this was cached
Debug.Print tbl.Columns(0).DefinedSize
' Previous line will fail if this info has not been cached
'Clean up
Set cat = Nothing
Set cnn = Nothing
Exit Sub
CloseConnectionError:
Set cat = 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
' EndCloseConnection2VB