HOWTO: Use Data Links to Create a Connection String at Run-Time

ID: Q218600


The information in this article applies to:


SUMMARY

This article demonstrates how to programmatically use Data Links feature of the Microsoft Data Access Components in order to generate a connection string at run-time.


MORE INFORMATION

In version 2.0 of the Microsoft Data Access Components, Data Links were introduced. Data Link files are similar to ODBC DSN files, but allow you to select an OLE DB provider to connect to your database. With the OLE DB Provider for ODBC drivers, you can also connect to an ODBC data source.

Double-clicking on a Data Link file displays a set of property pages that allow you to build a connection string to connect to your database.

You can use this same functionality in your Visual Basic applications by following the steps listed below:

  1. Launch Visual Basic and open a new Standard Exe project. Form1 is created by default.


  2. Select References from the Project menu, and then select Microsoft OLE DB Service Component 1.0 Type Library from the list of available references.


  3. Add a CommandButton to your form.


  4. Add the following code to the Click event of your CommandButton:


  5. 
    Private Sub Command1_Click()
        Dim objDataLink As New DataLinks
        Dim strConn As String
        
        strConn = objDataLink.PromptNew
        MsgBox "The connection string you created is:" & _
               vbCrLf & strConn
    End Sub 

  6. Run the project.



When you click the CommandButton, you will see the Data Links property pages. Once you have specified how you want to connect to your database and click the OK button, you'll see the connection string in a dialog box.


REFERENCES

For additional information about data links, please see the following article in the Microsoft Knowledge Base:

Q189680 HOWTO: Use Data Link Files with ADO 2.0

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by David Sceppa, Microsoft Corporation

Additional query words: datalink


Keywords          : kbADO200 kbADO210 kbMDAC kbOLEDB200 kbVBp500 kbVBp600 kbGrpVB 
Version           : WINDOWS:2.0,2.1,5.0,6.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: February 26, 1999