+ Reply to Thread
Results 1 to 5 of 5

SQL Query with Excel VBA Returns General ODBC Error when Using Variables

  1. #1
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    SQL Query with Excel VBA Returns General ODBC Error when Using Variables

    I have set up an SQL query of an Excel Worksheet using VBA in Excel. The query works when I "hardcode" the search string, but when I use a variable, I get the following error:

    Run Time Error: '1004'
    General ODBC Error.

    Here is the code with the variable xCaseNo.


    Please Login or Register  to view this content.
    If I supply the CaseNo by typing it in a string enclosed in single quotes, the query works, as in:

    Please Login or Register  to view this content.
    When I get the error message the oQt.Refresh line is highlighted in yellow.

    I suspected the problem may be related to SQL requiring quotes around the search string, so I defined xCaseNo as
    Please Login or Register  to view this content.
    , but that didn't work either.

    Thanks.
    Last edited by Dirigo; 10-31-2011 at 10:44 PM.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: SQL Query with Excel VBA Returns General ODBC Error when Using Variables

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    Re: SQL Query with Excel VBA Returns General ODBC Error when Using Variables

    I tried your suggestion, Kenneth, but I got a compile error: Expected End of Expression.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: SQL Query with Excel VBA Returns General ODBC Error when Using Variables

    I normally set the concatenated string to a variable and check for syntax. e.g.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-02-2011
    Location
    Los Angeles, USA
    MS-Off Ver
    2010 Home Version (doesn't include Access and Outlook )
    Posts
    22

    Re: SQL Query with Excel VBA Returns General ODBC Error when Using Variables

    Thanks, Kenneth. That debug.print suggestion really helped. I was able to see how the quotes weren't coming together. Problem solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1