tags:

views:

2142

answers:

3

Hi all,

I want to use the standard ADO connection string dialog box in MS Access. How can I do that?

A: 

See How to bind Microsoft Access forms to ADO recordsets

Galwegian
The link is interesting but ... it does not answer the question.
Philippe Grondier
+1  A: 

If not already selected, check the following references in the References dialog:

Microsoft OLE DB Service Component 1.0 Type Library

Microsoft ActiveX Data Objects 2.7 Library

The following code will open the dialog box and set a connection object to the parameters provided in the Data Link Properties dialog box:

Dim dl As MSDASC.DataLinks
Dim cn As ADODB.Connection

Set dl = New MSDASC.DataLinks
Set cn = New ADODB.Connection

Set cn = dl.PromptNew
cn.Open

You can then open an ADODB recordset with the connection or retrieve properties about the connection and proceed as needed.

John Mo
IIRC you need an On Error... statement before the call to PromptNew and test its return value for empty, being the only way to detect the that user has cancelled the dialog.
onedaywhen
+1  A: 

In addition to prerequisites as per John Mo, the following function will return you a full ADO connection string. It can be initiated with a default 'm_connectionString' value. Such a value can be stored in a 'connections table'.

Function connectToDababase(Optional m_connectionString As String) As String
Dim dl As MSDASC.DataLinks
Dim cn As ADODB.Connection

Set dl = New MSDASC.DataLinks
Set cn = New ADODB.Connection

If IsMissing(m_connectionString) Then
    Set cn = dl.PromptNew
Else
    cn.ConnectionString = m_connectionString
    dl.PromptEdit cn
End If

connectToDababase = cn.ConnectionString
End Function
Philippe Grondier
Your optional parameter m_connectionString needs to be declared as Variant to be able to test its absence using IsMissing(). If declared as a string and omitted by the caller, the value of m_connectionString will be empty string and not be 'missing'.
onedaywhen
You are right! But I did not detect it as the result is the same: the connection dialog opens with blank values ...
Philippe Grondier