Re-writing ODBC connection string

percy83

Active Member
Joined
Mar 11, 2009
Messages
278
Hi guys,

I have this code that has always worked for me in the past. It simply loops through all sheets looking for querytables and then re-writes the connection string. I think I initially had it done for Excel 2003 but it should also work for 2010 right?! The table is also a list object. Here is the code I am using:

Code:
Dim qt As QueryTableDim wks As Worksheet


Dim strNewFilePath As String
Dim strNewFileName As String


strNewFilePath = ThisWorkbook.Path
strNewFileName = ThisWorkbook.Name


MsgBox strNewFilePath & strNewFileName




For Each wks In ActiveWorkbook.Worksheets
For Each qt In wks.QueryTable


'MsgBox qt.Connection


With qt
    .Connection = Join$(Array("ODBC;DSN=Excel Files;DBQ=", _
    strNewFilePath & Application.PathSeparator & strNewFileName, _
    ";DefaultDir=", strNewFilePath, _
    ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"), vbNullString)


    .Refresh BackgroundQuery:=False


End With


Next qt
Next wks
Set qt = Nothing
Set wks = Nothing


Any ideas on what might be wrong?

Have a great week-end!
Per
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Andrew and thanks for your reply,

Sorry, I must have missed that when pasting the code here. The "s" is there in my code. The code executes and loops fine but it seems like it doesnt find the query table at all. Could it be stored/defined as something else?
 
Upvote 0
Thanks I got it worked out.

It was now stored as a workbook connection instead of a query table. I don't really know why but it could have something to do with the fact that the query itself is just from another sheet from the same workbook.

Thanks again for your help!
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

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