Senin, 02 Mei 2016

Microsoft Data Links



Microsoft Data Links provides a graphical user interface that enables the user to create, edit, and organize connections to a data source. A Data Link file for c:\nwind.mdb can be created as follows:
1.    In Windows Explorer, select the folder in which you want to create the new data link. For example, select the C:\ folder to create the data link file in the root directory of the C drive.
2.    Choose New from the File menu of Windows Explorer.
3.    Choose Microsoft Data Link.
4.    Rename the file nwind.udl.
5.    Double-click on the new file to open the Data Link Properties window.
6.    Select the Provider tab.
7.    Select "Microsoft Jet 4.0 OLE DB Provider" from the list.
8.    Select the Connection tab.
9.    Enter the path to the Northwind database (for example, c:\nwind.mdb) in the first text box.
The following code shows how to use the data link to open the connection rather than providing the connection information directly. 


Sub ADOUseExistingDataLink() 
   ' Opens an ADO Connection using a Data Links file (UDL)

   Dim cnn As New ADODB.Connection
   Dim rs As New ADODB.Recordset

   cnn.Open "File Name=.\NorthWind.udl;"

   rs.Open "Customers", cnn, adOpenKeyset, adLockReadOnly

   rs.MoveLast
   Debug.Print rs.RecordCount

   rs.Close
   cnn.Close

End Sub

It is also possible to use Microsoft Data Links to prompt the user for connection information. The following code demonstrates how to launch the Microsoft Data Links UI from code. In order to run this code, you'll need to add a reference to Microsoft OLE DB Service Component 1.0 Type Library in your project.
Private Sub Command1_Click()

   Dim cnn     As New ADODB.Connection
   Dim dl      As New DataLinks

   On Error Resume Next

   dl.hWnd = Me.hWnd
   If dl.PromptEdit(cnn) Then
      cnn.Open
   End If

   cnn.Close

End Sub

The previous code example could be modified to first specify a default value for the provider and data source.
Private Sub Command2_Click()

   Dim cnn     As New ADODB.Connection
   Dim dl      As New DataLinks

   On Error Resume Next

   cnn.Provider = "Microsoft.Jet.OLEDB.4.0;"
   cnn.Properties("Data Source") = ".\NorthWind.mdb"
   dl.hWnd = Me.hWnd
   If dl.PromptEdit(cnn) Then
      cnn.Open
   End If

   cnn.Close

End Sub


EmoticonEmoticon