I think my query code was wrong but it still works.

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Gurus,
I recorded a macro and changed it to a loop.
Below is the part where I think should have gone wrong. Basically I forgot to use variable string qname(qname is from day1 to day10) to replace "day1" in the "Data Source=$Workbook$;Location=day1" part.
However, the code ran and gave me the right tables from datasource. I tried leaving "location=" blank or changing it to some random names and the code can't proceed and returns "can't find xxx query" error.
So this means that "Location=day1" corresponds to qname right?
But if it does correspond with qname, which is from day1 to day10 generating 10 different queries, why was code ran without error returning and feeding me the right tables????


VBA Code:
ActiveWorkbook.connections.Add2 _
            qname, _
            "", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=day1;Extended Properties=" _
            , qname, 6, True, False
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
So I tried changing "location=" to different "dayxxx". As long as it is one of the "day1", "day2", "day3" ..... "day10" queries, it doesn't matter. What is the mechanism behind the code?????
As to me, "Data Source=$Workbook$;Location=day1" means that in the previous steps, queries are formed and stored in this workbook. and location should literally be the name of the query. Anyways I'm super confused...
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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