Relative MS Query?

mhenk

Well-known Member
Joined
Jun 13, 2005
Messages
591
I've got a few connections made to various datasources via MS Query (using XL 2007). I'd like to make these connections relative.

So, instead of pointing to a specific path and filename, I'd like the connections to be to a specific filename in the same directory as the excel file.

Is this possible?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi.

There are two properties of each query table that need to be set for this - the connection and the SQL (aka command text).

There is no built in functionality to do what you want (though I am unfamiliar with Excel 2007 I assume this is still the case in that version). To do so is best done via VBA - though it is not strictly required, it is best.

Both the connection and SQL are read/write properties and are purely text strings that can be easily manipulated.

By using the macro recorder whilst making a query table, you can obtain some code for Excel 2007 query tables. Then either post that here (between code tags) or use that as a basis for writing your own code to do what you want.

Along the lines.
Code:
'pseudo code, NOT Excel 2007 specific
dim qt as querytable
dim wks as worksheet
 
for each wks in activeworkbook.worksheets
for each qt in wks.querytables
 
'set connection, maybe using REPLACE$
qt.connection = ...
 
'and SQL
'maybe just a text manipulation to remove file path?
qt.SQL = ...
 
qt.refresh backgroundquery:=false
 
next qt
next wks
 
set qt=nothing
set wks=nothing
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,263
Members
448,881
Latest member
Faxgirl

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