Connections-Sammlung (DAO)
Gilt für: Access 2013, Office 2013
Hinweis
[!HINWEIS] ODBCDirect-Arbeitsbereiche werden in Microsoft Access 2013 nicht unterstützt. Verwenden Sie ADO, wenn Sie auf externe Datenquellen zugreifen möchten, ohne das Microsoft Access-Datenbankmodul zu verwenden.
Eine Connections-Auflistung enthält die aktuellen Connection-Objekte eines Workspace-Objekts. (Gilt nur für ODBCDirect-Arbeitsbereiche.)
Hinweise
Wenn Sie ein Connection-Objekt öffnen, wird es automatisch an die Connections-Auflistung des Workspace-Objekts angefügt. Wenn Sie ein Connection-Objekt über die Close -Methode schließen, wird es aus der Connections-Auflistung entfernt. Bevor Sie das Objekt schließen, sollten Sie alle geöffneten Recordset -Objekte im Connection-Objekt schließen.
Wenn Sie ein Connection-Objekt öffnen, wird gleichzeitig ein entsprechendes Database -Objekt erstellt und an die Databases -Auflistung in demselben Workspace-Objekt angefügt (und umgekehrt). Und wenn Sie das Connection-Objekt schließen, wird das entsprechende Database-Objekt aus der Databases-Auflistung entfernt usw.
Die Einstellung der Name-Eigenschaft eines Connection-Objekts ist eine Zeichenfolge, die den Pfad zur Datenbankdatei angibt. Der Verweis auf ein Connection-Objekt in einer Auflistung erfolgt über dessen Ordnungszahl oder den Wert der Name-Eigenschaft, wobei Sie die folgenden Syntaxformen verwenden können:
Connections(0)
Connections("Name")
Connections! [Name]
Hinweis
[!HINWEIS] Sie können dieselbe Datenquelle mehrmals öffnen, wodurch Duplikatnamen in der Connections-Auflistung erstellt werden. Sie sollten Objektvariablen Connection-Objekte zuweisen und mit Variablennamen auf sie verweisen.
Beispiel
In diesem Beispiel werden das Connection-Objekt und die Connections-Auflistung veranschaulicht, indem ein Database-Objekt und zwei Connection-ODBCDirect-Objekte geöffnet und die für die Objekte verfügbaren Eigenschaften aufgeführt werden.
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
In diesem Beispiel wird die OpenConnection-Methode mit verschiedenen Parametern verwendet, um drei verschiedene Connection-Objekte zu öffnen.
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