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

Posted on 2014-08-19
Last Modified: 2014-08-25
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.

Question by:Ray
    LVL 1

    Expert Comment

    Try changing the double quotes to single quotes.
    LVL 10

    Author Comment

    Those double quotes are a requirement of VBA and are used to encapsulate the text string on each line.  Can't remove them.
    LVL 37

    Accepted Solution

    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
    LVL 33

    Expert Comment

    Couldn't you just use this?
    SQL = " SELECT * FROM cust.job(SAV0" & Range("B3").Value & ") ; "

    Open in new window

    LVL 10

    Author Closing Comment

    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.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
    This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
    This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now