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.
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.
- In Visual Basic 6.0, add a reference to the Microsoft ActiveX
Data Objects Library.
- 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
- Set references to the Microsoft OLE DB Service Component 1.0 Type Library and Microsoft ActiveX Data Objects Library.
- 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