way to loop through all ADODB connections in a workbook

mbpmbp

New Member
Joined
Jul 28, 2015
Messages
8
Hi everyone,

I'm trying to operate a macro that loops through all connections strings in a workbook and defines them to their base form as defined in "DBPath" range below. Is there any way to do this? I get a run time error '13' type mismatch with what I'm currently trying below. Any help would be much appreciated!

Sub ADODBconnectionreset()


Dim cn As ADODB.Connection
Dim DBpath As String


'updates connection path to path defined in DBPath to keep workbook compatible across multiple versions of Excel



DBpath = ThisWorkbook.Worksheets("ProjectDetails").Range("DBPath").Value
For Each cn In ThisWorkbook.Connections

cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Mode=Read; Data Source=" & DBpath & ";"

Next cn




End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The Workbook.Connections collection contains WorkbookConnection objects, not ADODB.Connection objects.
 
Upvote 0
Hi
Try
Code:
    Dim cn As WorkbookConnection
    Dim olecn As OLEDBConnection
    For Each cn In ThisWorkbook.Connections
        If cn.Type = xlConnectionTypeOLEDB Then
            Set olecn = cn.OLEDBConnection
            olecn.Connection = "Provider=Microsoft.ACE.OLEDB.12.0; Mode=Read; Data Source=" & DBpath & ";"
        End If
    Next
Regards,
 
Upvote 0
Thanks so much for your help!

I'm getting a run time error 1004 application-defined or object-defined error on the line:

olecn.Connection = "Provider=Microsoft.ACE.OLEDB.12.0; Mode=Read; Data Source=" & DBpath & ";"

Any ideas around that?
 
Upvote 0
Sorry, I tested my code and received some mistake. It is needed to add "OLEDB;" instruction to a connection string. Try with
Code:
olecn.Connection = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0; Mode=Read; Data Source=" & DBpath & ";"
It works
Regards,
 
Upvote 0
Yes, it works great now, thanks so much!

The next issue I'm running into is that it still wants to rewrite the connections when I run another macro saying that the the data file is not found and asking me to connect to the exact same file in the exact same path instead. Any ideas around this one?
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,690
Members
449,117
Latest member
Aaagu

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