OpenConnection Method Example

This example uses the OpenConnection method with different parameters to open three different Connection objects.

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..."
   Set conPubs = wrkODBC.OpenConnection("Connection1", _
      dbDriverNoPrompt, , _
      "ODBC;DATABASE=pubs;UID=sa;PWD=;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