Pass global VBA variable to Power Query

StaticVoid

New Member
Joined
Oct 24, 2014
Messages
10
Hello!

I have a query to pass to mySQL database through Power Query. In essence, it looks like this:
Code:
SELECT *
FROM table
WHERE
col1 LIKE "%abc%" OR
col1 LIKE "%xyz%" OR
....

It is a part of more complex query that joins several tables . In my workbook I have a list of names ("abc", "xyz" etc.). Loading entire table and matching it in power query is not an option, because it is too large.

My idea was to concatenate the names in one cell, make a dynamic range and refer them in PowerQuery:
Code:
QueryPart = Excel.CurrentWorkbook(){[Name="MyTable"]}[Content][MyColumn]{0}
Source = MySQL.Database("server", "database", [Query="SELECT * FROM table WHERE " & QueryPart])
It is secure since database only allows select queries.

The problem is that concatenated names often exceed maximum number of characters allowed in Excel cell, so I'm looking for a way to pass a VBA string variable to PQ. Any alternative solutions are welcome as well.

I'm using Excel 2010 Pro Plus.

Thank you!
 
Hello Ken. Sorry about the late response.

Your solution is exactly what I needed, thank you very much!

I've re-written your function (not a function any more), to concatenate all the columns. Interestingly, your function worked for me, but it ended up with abcdefgh,,,,,,,,
Yeah, that makes sense now that I look at it. I wonder why I'm getting Null Reference Exception though. Just out of curiosity, are you using Excel 2013 or 2010?
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
StaticVoid said:
Your solution is exactly what I needed, thank you very much!
Awesome! :)

StaticVoid said:
I wonder why I'm getting Null Reference Exception though.
Honestly, not sure. I'd probably need to see the real data to figure that one out. I suppose it could be from having too many records (exhausting memory) or having some bad data in there. Both of those are just hypothetical though, and without seeing the real goods it's hard to know. If you want to find out, try chopping your data in half or just using 10 records to see if it persists.

StaticVoid said:
Just out of curiosity, are you using Excel 2013 or 2010?
I'm using Excel 2013. I just tested it in Excel 2010 as well though. Strangely, it gagged at first, but when I refreshed it again it came through with the same result as before (abcdefgh,,,,,,,)
 
Upvote 0
Honestly, not sure. I'd probably need to see the real data to figure that one out.
I'm getting this Null Reference Exception on "abcdefgh" data as well. Must be some problem on Microsoft side.
it came through with the same result as before (abcdefgh,,,,,,,)
I think I got this part wrong
Code:
if (nrows - 1) > idx then col{idx} & @recConcat2(col, (idx + 1), sep, nrows) & sep
should be
Code:
if (nrows - 1) > idx then col{idx} & sep & @recConcat2(col, (idx + 1), sep, nrows)
Hard to debug when I can't see the result.

But anyway, your solution works and that's all that really matters. Thanks again, Ken.
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,058
Members
449,206
Latest member
Healthydogs

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