sql query gives error via vba but exact query runs fine outside of vba

I use an ADO connection to a DB2 for i server to pull data into Excel.  I do this often so in general I'm pretty good at it ;-)

Recently I've encountered an error on a new query, or should I say query with multiple statements, that is erroring out.

As a matter of habit I always insert the 'string' of my sql that vba is executing to a cell in the workbook.  This way, If needed, I can verify all my variables (cell references) were passed as expected and I can troubleshoot from this.

In my current 'problem', I am getting the attached error even though I can copy/paste the exact code from the spreadsheet into my query tool and run without issue.

Below this you will find the code I use to build my string that returns the error.  Note that this includes a semicolon as required between execution 'pieces' as required by SQL.
exact VBA
SQL = " CREATE ALIAS temp.kd2 FOR cust.job(SAV0" & Range("B3").Value & ") ; "
SQL = SQL + " select * from temp.kd2  ;"
SQL = SQL + " DROP ALIAS temp.kd2 ;   "

Exactly what I get from the cell where I have the 'sql' string put into.
 CREATE ALIAS temp.kd2 FOR cust.job(SAV0554158) ;  select * from temp.kd2  ;   DROP ALIAS temp.kd2 ;  

As I mentioned, running that exact query in my query tool works perfectly.  Thus, I know the query itself is fine.  There must be some issue with using ADO connections and multiple code execution or something else that I am missing.

I cross posted this because while I am using DB2, I do not believe this has anything to do with the DB since the query itself is valid.  I expect this has something to do with VBA and the ; symbol.

LVL 11
RayData AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try changing the double quotes to single quotes.
RayData AnalystAuthor Commented:
Those double quotes are a requirement of VBA and are used to encapsulate the text string on each line.  Can't remove them.
you are trying to execute 3 sql statements in one call
the query tool knows automatically to break the input on ; and execute the commands in order
try to execute the 3 commands on after the other and you should be good to go

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieVBA ExpertCommented:
Couldn't you just use this?
SQL = " SELECT * FROM cust.job(SAV0" & Range("B3").Value & ") ; "

Open in new window

RayData AnalystAuthor Commented:
Sadly, this is indeed the issue.  When running the query through Excel/VBA, you cannot execute multiple statements via the statement termination character.  

The workaround is to execute each one independently and in order.  I was afraid of this by the time I posted the question, but based on further research and the responses here... a Workaround is the only way to do it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.