OpenConnection method in an Access project does not work with UDL files in Access 2002 (305388)
The information in this article applies to:
This article was previously published under Q305388 Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies only to a Microsoft Access project (.adp).
SYMPTOMS
When you try to pass a valid UDL file as the BaseConnectionString argument of the CurrentProject.OpenConnection method in an Access project (ADP), you receive no error message, but the ADP file is not connected.
CAUSE
The BaseConnectionString argument of the OpenConnection method does not accept UDL files.
RESOLUTION
To work around this behavior, use the UDL file to open an ADO connection, and then pass the connection string information of the newly opened connection object as the BaseConnectionString argument of the CurrentProject.OpenConnection method. For an example of how to do so, follow these steps:
- Create a UDL file that has connection information for a valid SQL Server using the Microsoft OLEDB driver for SQL Server (see the "More Information" section later in this article for instructions on how to create a UDL file). Save the file as Test.udl.
- Open the sample project NorthwindCS.adp.
- In the Database window, click Forms under Objects, click New, and then click OK to open a new form in Design view.
- Add a command button to the new form.
- Set the OnClick property of the command button to the following event procedure:
On Error GoTo EH:
Application.CurrentProject.CloseConnection
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
'Change the following path to your actual path to Test.udl
cnn.Open "File Name=C:\Path\Test.udl;"
Application.CurrentProject.OpenConnection cnn.ConnectionString
cnn.Close
Set cnn = Nothing
If CurrentProject.IsConnected Then
'It worked.
MsgBox "You are now connected using a UDL file"
Else
'It didn't work.
MsgBox "Unable to Connect using the specified UDL file"
End If
Exit Sub
EH:
MsgBox Err.Number & ": " & Err.Description
- Close the Visual Basic Environment to return to the form.
- Save the form, and then open it in Form view.
- Click the command button to run the underlying code.
Note that you receive a message box that indicates success.
Modification Type: | Major | Last Reviewed: | 8/10/2004 |
---|
Keywords: | kbProgramming KbVBA kbdta kbprb KB305388 |
---|
|