colección Connections (DAO)
Se aplica a: Access 2013, Office 2013
Nota:
[!NOTA] Las áreas de trabajo de ODBCDirect no se admiten en Microsoft Access 2013. Use ADO si desea obtener acceso a orígenes de datos externos sin usar el motor de base de datos de Microsoft Access.
Una colección Connections contiene los objetos Connection actuales de un objeto Workspace. (solo áreas de trabajo de ODBCDirect).
Comentarios
Al abrir un objeto Connection, éste se anexa automáticamente a la colección Connections del objeto Workspace. Al cerrar un objeto Connection con el método Close, este objeto se quita de la colección Connections. Debe cerrar todos los objetos Recordset abiertos dentro de Connection antes de cerrar el objeto.
Al mismo tiempo que abre un objeto Connection, se crea el objeto Database correspondiente y se agrega a la colección Databases en el mismo objeto Workspace, y viceversa. De igual forma, al cerrar el objeto Connection, se elimina el objeto Database correspondiente de la colección Databases, y viceversa.
El valor de la propiedad Name de un objeto Connection es una cadena que especifica la ruta del archivo de base de datos. Para hacer referencia a un objeto Connection en una colección mediante su número ordinal o mediante el valor de la propiedad Name, utilice una de las formas sintácticas siguientes:
Connections(0)
Connections("nombre")
Connections! [nombre]
Nota:
[!NOTA] Puede abrir el mismo origen de datos varias veces creando nombres duplicados en la colección Connections. Debe asignar objetos Connection a variables de objeto y hacer referencia a ellas mediante el nombre de variable.
Ejemplo
En este ejemplo se utiliza el objeto Connection y la colección Connections para abrir un objeto Database y dos objetos Connection de ODBCDirect y especificar las propiedades disponibles para cada objeto.
Sub ConnectionObjectX()
Dim wrkAcc as Workspace
Dim dbsNorthwind As Database
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim conPubs2 As Connection
Dim conLoop As Connection
Dim prpLoop As Property
' Open a Database object.
Set wrkAcc = CreateWorkspace("NewWorkspace", _
"admin", "", dbUseJet)
Set dbsNorthwind = wrkAcc.OpenDatabase("Northwind.mdb")
' Create ODBCDirect Workspace object and open Connection
' objects.
Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _
"admin", "", dbUseODBC)
' Note: The DSNs referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Connection1", , , _
"ODBC;DATABASE=pubs;DSN=Publishers")
Set conPubs2 = wrkODBC.OpenConnection("Connection2", , _
True, "ODBC;DATABASE=pubs;DSN=Publishers")
Debug.Print "Database properties:"
With dbsNorthwind
' Enumerate Properties collection of Database object.
For Each prpLoop In .Properties
On Error Resume Next
Debug.Print " " & prpLoop.Name & " = " & _
prpLoop.Value
On Error GoTo 0
Next prpLoop
End With
' Enumerate the Connections collection.
For Each conLoop In wrkODBC.Connections
Debug.Print "Connection properties for " & _
conLoop.Name & ":"
With conLoop
' Print property values by explicitly calling each
' Property object; the Connection object does not
' support a Properties collection.
Debug.Print " Connect = " & .Connect
' Property actually returns a Database object.
Debug.Print " Database[.Name] = " & _
.Database.Name
Debug.Print " Name = " & .Name
Debug.Print " QueryTimeout = " & .QueryTimeout
Debug.Print " RecordsAffected = " & _
.RecordsAffected
Debug.Print " StillExecuting = " & _
.StillExecuting
Debug.Print " Transactions = " & .Transactions
Debug.Print " Updatable = " & .Updatable
End With
Next conLoop
dbsNorthwind.Close
conPubs.Close
conPubs2.Close
wrkAcc.Close
wrkODBC.Close
End Sub
En este ejemplo se utiliza el método OpenConnection con distintos parámetros para abrir tres objetos Connection diferentes.
Sub OpenConnectionX()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim conPubs2 As Connection
Dim conPubs3 As Connection
Dim conLoop As Connection
' Create ODBCDirect Workspace object.
Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _
"admin", "", dbUseODBC)
' Open Connection object using supplied information in
' the connect string. If this information were
' insufficient, you could trap for an error rather than
' go to an ODBC Driver Manager dialog box.
MsgBox "Opening Connection1..."
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Connection1", _
dbDriverNoPrompt, , _
"ODBC;DATABASE=pubs;DSN=Publishers")
' Open read-only Connection object based on information
' you enter in the ODBC Driver Manager dialog box.
MsgBox "Opening Connection2..."
Set conPubs2 = wrkODBC.OpenConnection("Connection2", _
dbDriverPrompt, True, "ODBC;DSN=Publishers;")
' Open read-only Connection object by entering only the
' missing information in the ODBC Driver Manager dialog
' box.
MsgBox "Opening Connection3..."
Set conPubs3 = wrkODBC.OpenConnection("Connection3", _
dbDriverCompleteRequired, True, _
"ODBC;DATABASE=pubs;DSN=Publishers;")
' Enumerate the Connections collection.
For Each conLoop In wrkODBC.Connections
Debug.Print "Connection properties for " & _
conLoop.Name & ":"
With conLoop
' Print property values by explicitly calling each
' Property object; the Connection object does not
' support a Properties collection.
Debug.Print " Connect = " & .Connect
' Property actually returns a Database object.
Debug.Print " Database[.Name] = " & _
.Database.Name
Debug.Print " Name = " & .Name
Debug.Print " QueryTimeout = " & .QueryTimeout
Debug.Print " RecordsAffected = " & _
.RecordsAffected
Debug.Print " StillExecuting = " & _
.StillExecuting
Debug.Print " Transactions = " & .Transactions
Debug.Print " Updatable = " & .Updatable
End With
Next conLoop
conPubs.Close
conPubs2.Close
conPubs3.Close
wrkODBC.Close
End Sub