+ Reply to Thread
Results 1 to 16 of 16

SQL Syntax Error

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    SQL Syntax Error

    Hi Everyone,

    I'm creating a macro which uses a query to search a database using two variables to define the search. When I get to the line .Refresh BackgroundQuery:=False (Highlighted in code), I get a SQL Syntax error message. Here's my code, any ideas why this is happening?
    Sub RefreshTable()
    '
    ' RefreshTable Macro
    '
    
    '
    Dim from As Date
    Dim untill As Date
    
    from = Range("C2").Value
    untill = Range("D2").Value
        Sheets("Sheet1").Select
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
            "ODBC;DSN=AR System ODBC Data Source;ARServer=iu-kjggg8.co.uk;ARServerPort=6512;UID=pfrhh;;ARAuthentication=;ARU" _
            ), Array("seUnderscores=1;SERVER=NotTheServer")), Destination:=Range( _
            "Sheet1!$A$1")).QueryTable
            .CommandText = Array( _
            "SELECT HPD_Help_Desk.Incident_Number, HPD_Help_Desk.Description, HPD_Help_Desk.First_Name, HPD_Help_Desk.Last_Name, HPD_Help_Desk.Priority, HPD_Help_Desk.Status, HPD_Help_Desk.SLM_Status, HPD_Help_Des" _
            , _
            "k.Company, HPD_Help_Desk.Last_Resolved_Date" & Chr(13) & "" & Chr(10) & "FROM HPD_Help_Desk HPD_Help_Desk" & Chr(13) & "" & Chr(10) & "WHERE (HPD_Help_Desk.Company='DCWR') AND (HPD_Help_Desk.Status>='Resolved') AND (HPD_Help_Desk.Last_Resolved_Date>{ts '20" _
            , _
            "from'} And HPD_Help_Desk.Last_Resolved_Date<{ts 'untill'})" _
            )
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Table_ExternalData_1"
            .Refresh BackgroundQuery:=False        
          End With
        ActiveSheet.ListObjects("Table_ExternalData_1").TableStyle = ""
        Range("B10").Select
    End Sub
    Thanks for your help!
    Last edited by JoWales; 02-25-2011 at 12:35 PM. Reason: To provide clarity of the issue

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: SQL Syntax Error

    You are passing the literal string 'untill' in your SQL, not concatenating the variable into it.

    Note: please use code tags whenever you post code as required by the forum rules. I have added them for you on this occasion but you must do it yourself in future, please.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Quote Originally Posted by romperstomper View Post
    You are passing the literal string 'untill' in your SQL, not concatenating the variable into it.
    So you're saying that currently I'm literally inserting the word "untill" into my query rather than the variable contained within "untill"? How do I refer to the variable then?


    Thanks for the heads up on the code tags, I'll do that in the future
    Last edited by JoWales; 02-25-2011 at 11:01 AM. Reason: Corrected a spelling mistake

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: SQL Syntax Error

    Correct. Instead of:
    "from'} And HPD_Help_Desk.Last_Resolved_Date<{ts 'untill'})" _
    you need:
    "from'} And HPD_Help_Desk.Last_Resolved_Date<{ts '" & untill & "'})" _

  5. #5
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Ok thanks. I've edited the code as you suggested but I am still receiving the same error. I also then changed the code where the other variable (from) is used , as I suspected it had the same error, from:

    "from'} And HPD_Help_Desk.Last_Resolved_Date<{ts '" & untill & "'})" _

    To:

    "'" & from & "'} And HPD_Help_Desk.Last_Resolved_Date<{ts '" & untill & "'})" _

    But still no luck. Any other advice on what the problem may be? Thanks again.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: SQL Syntax Error

    Your From date check appears to have a random '20' on the front of it, which won't be helping. I also can't see the point of aliasing a table with its actual name. Try using:
    .CommandText = "SELECT HPD_Help_Desk.Incident_Number, HPD_Help_Desk.Description, HPD_Help_Desk.First_Name, " & _
             "HPD_Help_Desk.Last_Name, HPD_Help_Desk.Priority, HPD_Help_Desk.Status, HPD_Help_Desk.SLM_Status, " & _
             "HPD_Help_Desk.Company, HPD_Help_Desk.Last_Resolved_Date FROM HPD_Help_Desk " & _
             "WHERE (HPD_Help_Desk.Company='DCWR') AND (HPD_Help_Desk.Status>='Resolved') " & _
             "AND (HPD_Help_Desk.Last_Resolved_Date>{ts '" & from & "'} " & _
             "And HPD_Help_Desk.Last_Resolved_Date<{ts '" & untill & "'})"
    and see if that helps.

  7. #7
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Thanks again, but it's still showing the same error

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: SQL Syntax Error

    Maybe date format - try:
    .CommandText = "SELECT HPD_Help_Desk.Incident_Number, HPD_Help_Desk.Description, HPD_Help_Desk.First_Name, " & _
             "HPD_Help_Desk.Last_Name, HPD_Help_Desk.Priority, HPD_Help_Desk.Status, HPD_Help_Desk.SLM_Status, " & _
             "HPD_Help_Desk.Company, HPD_Help_Desk.Last_Resolved_Date FROM HPD_Help_Desk " & _
             "WHERE (HPD_Help_Desk.Company='DCWR') AND (HPD_Help_Desk.Status>='Resolved') " & _
             "AND (HPD_Help_Desk.Last_Resolved_Date>{ts '" & Format(from, "yyyy-mm-dd") & "'} " & _
             "And HPD_Help_Desk.Last_Resolved_Date<{ts '" & Format(untill, "yyyy-mm-dd") & "'})"

  9. #9
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Unfortunately not. I tried other date formats too to no avail.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: SQL Syntax Error

    Have you checked the values of From and Untill? Also, have you tried recording a macro of setting up this connection? (or is that what this is - if so, what was the original code?)

  11. #11
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Quote Originally Posted by romperstomper View Post
    Have you checked the values of From and Untill? Also, have you tried recording a macro of setting up this connection? (or is that what this is - if so, what was the original code?)
    From and untill are in the format dd/mm/yyy and seem ok to me. Here is the original code of the recording:


        Sheets("Sheet1").Select
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
            "ODBC;DSN=AR System ODBC Data Source;ARServer=os-service-management.uk.logica.com;ARServerPort=6512;UID=pricejo;;ARAuthentication=;ARU" _
            ), Array("seUnderscores=1;SERVER=NotTheServer")), Destination:=Range("$A$1")). _
            QueryTable
            .CommandText = Array( _
            "SELECT HPD_Help_Desk.Incident_Number, HPD_Help_Desk.Description, HPD_Help_Desk.Company, HPD_Help_Desk.First_Name, HPD_Help_Desk.Last_Name, HPD_Help_Desk.Priority, HPD_Help_Desk.Status, HPD_Help_Desk.S" _
            , _
            "LM_Status, HPD_Help_Desk.Last_Resolved_Date" & Chr(13) & "" & Chr(10) & "FROM HPD_Help_Desk HPD_Help_Desk" & Chr(13) & "" & Chr(10) & "WHERE (HPD_Help_Desk.Company='DCWR') AND (HPD_Help_Desk.Status>='Resolved') AND (HPD_Help_Desk.Last_Resolved_Date>{ts '20" _
            , _
            "11-01-01 00:00:00'} And HPD_Help_Desk.Last_Resolved_Date<{ts '2011-02-26 00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY HPD_Help_Desk.Last_Resolved_Date, HPD_Help_Desk.First_Name" _
            )
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Table_Query_from_AR_System_ODBC_Data_Source"
            .Refresh BackgroundQuery:=False
        End With
        ActiveWindow.SmallScroll Down:=-15
    End Sub

    I just re-made this because I didn't have a copy of the original code dorry, but I followed the same steps in creating this so it should be the same.

    I replaced the dates with the variables from and untill again but no luck.

  12. #12
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: SQL Syntax Error

    Should it be "d" instead of "ts" since there's no time?
    >{d '" & Format(from, "yyyy-mm-dd") & "'} " & _
    <{d '" & Format(untill, "yyyy-mm-dd") & "'})"
    Hope that helps,

    Colin

    RAD Excel Blog

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: SQL Syntax Error

    Based on that and Colin's post, you probably need:
    .CommandText = "SELECT HPD_Help_Desk.Incident_Number, HPD_Help_Desk.Description, HPD_Help_Desk.First_Name, " & _
             "HPD_Help_Desk.Last_Name, HPD_Help_Desk.Priority, HPD_Help_Desk.Status, HPD_Help_Desk.SLM_Status, " & _
             "HPD_Help_Desk.Company, HPD_Help_Desk.Last_Resolved_Date FROM HPD_Help_Desk " & _
             "WHERE (HPD_Help_Desk.Company='DCWR') AND (HPD_Help_Desk.Status>='Resolved') " & _
             "AND (HPD_Help_Desk.Last_Resolved_Date>{ts '" & Format(from, "yyyy-mm-dd hh:mm:ss") & "'} " & _
             "And HPD_Help_Desk.Last_Resolved_Date<{ts '" & Format(untill, "yyyy-mm-dd" hh:mm:ss) & "'})"

  14. #14
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Awesome! That did it! Thanks so much guys, I really appreciate it. I'll post the finished code so that anyone who reads this in the future can see the final version:


    Sub RefreshTable()
    '
    ' RefreshTable Macro
    '
    
    '
    Dim from As Date
    Dim untill As Date
    
    from = Range("C2").Value
    untill = Range("D2").Value
        Sheets("Sheet1").Select
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
            "ODBC;DSN=AR System ODBC Data Source;ARServer=kj-udyd.co.uk;ARServerPort=6512;UID=hillja;;ARAuthentication=;ARU" _
            ), Array("seUnderscores=1;SERVER=NotTheServer")), Destination:=Range( _
            "Sheet1!$A$1")).QueryTable
             .CommandText = "SELECT HPD_Help_Desk.Incident_Number, HPD_Help_Desk.Description, HPD_Help_Desk.First_Name, " & _
             "HPD_Help_Desk.Last_Name, HPD_Help_Desk.Priority, HPD_Help_Desk.Status, HPD_Help_Desk.SLM_Status, " & _
             "HPD_Help_Desk.Company, HPD_Help_Desk.Last_Resolved_Date FROM HPD_Help_Desk " & _
             "WHERE (HPD_Help_Desk.Company='DCWR') AND (HPD_Help_Desk.Status>='Resolved') " & _
             "AND (HPD_Help_Desk.Last_Resolved_Date>{ts '" & Format(from, "yyyy-mm-dd hh:mm:ss") & "'} " & _
             "And HPD_Help_Desk.Last_Resolved_Date<{ts '" & Format(untill, "yyyy-mm-dd hh:mm:ss") & "'})"
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Table_ExternalData_1"
            .Refresh BackgroundQuery:=False
          End With
        ActiveSheet.ListObjects("Table_ExternalData_1").TableStyle = ""
        Range("B10").Select
    End Sub

    Thanks again guys, you were a massive help.

  15. #15
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: SQL Syntax Error

    Hi Jo,

    One last, small detail. It doesn't matter in this case because the mm follows on after hh (so the Format function knows to use minutes instead of months), but I recommend that you get in the habit of using "n" for minutes.
    eg.
    Format(from, "yyyy-mm-dd hh:nn:ss")

  16. #16
    Registered User
    Join Date
    01-27-2011
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: SQL Syntax Error

    Ok Colin, thanks for the tip!

+ 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