+ Reply to Thread
Results 1 to 5 of 5

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

Hybrid View

Dirigo SQL Query with Excel VBA... 10-29-2011, 09:57 PM
Kenneth Hobson Re: SQL Query with Excel VBA... 10-30-2011, 01:06 AM
Dirigo Re: SQL Query with Excel VBA... 10-30-2011, 01:36 AM
Kenneth Hobson Re: SQL Query with Excel VBA... 10-30-2011, 02:27 AM
Dirigo Re: SQL Query with Excel VBA... 10-30-2011, 03:05 AM
  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.


    Sub CreateQT()
    
        Dim sConn As String
        Dim sSql As String
        Dim oQt As QueryTable
        Dim WS As Worksheet
        Dim xCaseNo As String
    
     xCaseNo = "123456"   
    sConn = "ODBC;DSN=Excel Files;DBQ=C:\Users\xxxx\Dropbox\Book1.xlsm;DefaultDir=C:\Users\xxxx\Dropbox;_
    DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"
    sSql = "SELECT `Clients$`.LastName, `Agents$`.LastName, `Agents$`.SerialNo, _
    `Agents$`.CaseNo FROM `Clients$` `Clients$`, `Agents$` `Agents$` _
    WHERE (`Agents`.CaseNo=xCaseNo);"
        
    Set oQt = Application.ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("a1"), Sql:=sSql)
        oQt.Refresh
    
    End Sub
    If I supply the CaseNo by typing it in a string enclosed in single quotes, the query works, as in:

    sSql = "SELECT `Clients$`.LastName, `Agents$`.LastName, `Agents$`.SerialNo,_
     `Agents$`.CaseNo FROM `Clients$` `Clients$`, `Agents$` `Agents$` WHERE _
    (`Agents`.CaseNo='123456');"
    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
    xCaseNo = (Chr(39) & xCaseNo & Chr(39))
    , 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

    DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"
    sSql = "SELECT `Clients$`.LastName, `Agents$`.LastName, `Agents$`.SerialNo, _
    `Agents$`.CaseNo FROM `Clients$` `Clients$`, `Agents$` `Agents$` _
    WHERE (`Agents`.CaseNo='" & xCaseNo) &';"

  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.
    Sub t()
      Dim sSQL As String, xCaseNo As String
      xCaseNo = "123456"
      sSQL = "SELECT `Clients$`.LastName, `Agents$`.LastName, `Agents$`.SerialNo, " & _
       "`Agents$`.CaseNo FROM `Clients$` `Clients$`, `Agents$` `Agents$` " & _
      "WHERE (`Agents`.CaseNo='" & xCaseNo & "';"
      Debug.Print sSQL
    End Sub

  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