Importing data using ODBC Database

murphyku

New Member
Joined
May 25, 2006
Messages
17
My goal here is to give the user a prompt (or userform) containing the different ODBC databases they can choose from. Once they choose one, I would like to retrieve data from that database.

I know how to retrieve the data from a specific ODBC database using VBA, but I am stumped as to how to give the user a list of the available ODBC databases to choose from and then use the chosen database to retrieve data.

The data that is to be retrieved is the same for each database. I would just like the user to have control over which database they are retrieving data from.

Any amount of help or ideas is appreciated.

Thanks!

murphyku
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can use the datalinks wizard

Code:
     '*************************************************************************
     '* The following code enables the user to get a valid connection string  *
     '* for use when returning data to Excel using ADO.                       *
     '*                                                                       *
     '* Requires references to the following:                                 *
     '* 1. Microsoft ActiveX Data Objects 2.x Library                         *
     '* 2. Microsoft OLE DB Service Component 1.0 Type Library                *
     '*                                                                       *

     
    Dim cnADO As ADODB.Connection 
    Dim objFinder As MSDASC.DataLinks 
    Dim strCon As String 
     
     'Set up reference to the relevant ADO Objects.
    Set objFinder = New MSDASC.DataLinks 
    Set cnADO = New ADODB.Connection 
     
    On Error Goto Err_stop 
     'Show the Database Connection wizard
    strCon = objFinder.PromptNew 
     'Test connection.
    cnADO.Open strCon 
     'show connectionstring in messagebox
    MsgBox strCon 
     'Print the connection string to the VBE Immediate Window.
    Debug.Print strCon 'or return this variable to your code
     'Clean up.
     
ExitPoint: 
    cnADO.Close 
    Set cnADO = Nothing 
    Set objFinder = Nothing 
    Exit Sub 
     
Err_stop: 
    If Err.Number = 91 Then 
        Resume ExitPoint 
    End If 
End Sub
 
Upvote 0
****EDIT****

Thank you very much, I have solved my problem using what you gave me and a few minor tweaks.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top