How To Create an ODBC and OLEDB Connection Prompt Control in ADO (193128)



The information in this article applies to:

  • ActiveX Data Objects (ADO) 1.0
  • ActiveX Data Objects (ADO) 1.5
  • ActiveX Data Objects (ADO) 2.0
  • ActiveX Data Objects (ADO) 2.1 SP2
  • ActiveX Data Objects (ADO) 2.5
  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7

This article was previously published under Q193128

SUMMARY

This article demonstrates how to prompt a user for ODBC connect string information such as Server, User ID, Password, Trusted_Connection, Language, and Database using ActiveX Data Objects (ADO).

The article also shows how to display an OLE DB Data Link dialog box to select an OLEDB provider and its properties.

MORE INFORMATION

ODBC Connect String

The following ADO example uses a "DSN-less" ODBC connection so you do not need to set up a data source name (DSN) with the ODBC Admin utility.
  1. In Visual Basic 6.0, add a reference to the Microsoft ActiveX Data Objects Library.
  2. Paste the following code into the Sub Form_Load() section of a new form or another place in your Visual Basic code:
          Dim conn As ADODB.Connection
          Dim strConn As String
    
          ' Assign the connection string to a variable.
    
          strConn = "DRIVER={SQL Server};SERVER=;UID=;PWD=;DATABASE="
    
          ' Create the Connection object.
          Set conn = New ADODB.Connection
    
          'Assign the connection string and provider, then open the connection.
          With conn
            .ConnectionString = strConn
            .Provider = "MSDASQL"
          ' Valid Enums for the ADO Prompt property are:
          ' adPromptAlways = 1
          ' adPromptComplete = 2
          ' adPromptCompleteRequired = 3
          ' adPromptNever =4
            .Properties("Prompt") = adPromptAlways
            .Open strConn
          End With
    					

OLEDB Data Link Dialog Box

  1. Set references to the Microsoft OLE DB Service Component 1.0 Type Library and Microsoft ActiveX Data Objects Library.
  2. Use the following code to open a data link dialog box and print the connection string in the immediate window. Add a Command button named btnRunTest and paste the following code into the Click event of the btnRunTest command button:
          Private Sub btnRunTest_Click()
          Dim conn As New ADODB.Connection
          Dim dl As New MSDASC.DataLinks
    
          conn = dl.PromptNew
          Debug.print conn.ConnectionString
    
          End Sub
    					

Modification Type:MinorLast Reviewed:3/14/2005
Keywords:kbDatabase kbhowto kbProvider KB193128