Configure a propriedade ActiveConnection como uma conexão válida e abra o catálogo. Torne possível acessar os objetos de esquema contidos em um catálogo aberto.
' BeginOpenConnectionVBSub OpenConnection()On Error GoTo OpenConnectionErrorDim cnn As New ADODB.ConnectionDim cat As New ADOX.Catalogcnn.Open "Provider='Microsoft.Jet.OLEDB.4.0';" & _"Data Source= 'c:\Program Files\Microsoft Office\" & _"Office\Samples\Northwind.mdb';"Set cat.ActiveConnection = cnnDebug.Print cat.Tables(0).Type'Clean upcnn.CloseSet cat = NothingSet cnn = NothingExit SubOpenConnectionError:Set cat = NothingIf Not cnn Is Nothing ThenIf cnn.State = adStateOpen Then cnn.CloseEnd IfSet cnn = NothingIf Err <> 0 ThenMsgBox Err.Source & "-->" & Err.Description, , "Error"End IfEnd Sub' EndOpenConnectionVB
Configurar a propriedade ActiveConnection como uma sequência de conexão válida também "abrirá" o catálogo.
Sub Main()On Error GoTo OpenConnectionWithStringErrorDim cat As New ADOX.Catalogcat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _"Data Source='c:\Program Files\Microsoft Office\" & _"Office\Samples\Northwind.mdb';"Debug.Print cat.Tables(0).Type'Clean upSet cat.ActiveConnection = NothingExit SubOpenConnectionWithStringError:Set cat = NothingIf Err <> 0 ThenMsgBox Err.Source & "-->" & Err.Description, , "Error"End IfEnd Sub' EndOpenConnection2VB
Tags: VBA, ActiveConnection, Tips, ADODB, OLEDB, ADOX,