1

Trying to edit some old code left by a previous employee and move to over to an updated system, but by simply copying the code that was wrote originally I get the Runtime Error '1004' - SQL Syntax Error. A copy of the code is below if anyone would like to point me in the right direction??

The part in bold seems to be where it errors...

Sub MIMacro()
'
'
    Sheets("MI_Report").Select
    Cells.Select
    Selection.ClearContents

    Dim StartDate As String

    Sheets("Date").Select

    StartDate = Range("D2").Value & "-" & Range("C2").Value & "-" & Range("B2").Value & " 00:00:00"

    Dim EndDate As String

    EndDate = Range("D3").Value & "-" & Range("C3").Value & "-" & Range("B3").Value & " 00:00:00"

    Sheets("MI_Report").Select

    Dim MySql As String
    MySql = "SELECT * FROM `W:\MI Reports\Imprint Reports.mdb`.XGSNOR_MI Katie L XGSNOR_MI Katie L WHERE (XGSNOR_MI Katie L.DelDate>={ts '" & StartDate & "'} And XGSNOR_MI Katie L.DelDate<={ts '" & EndDate & "'}) ORDER BY XGSNOR_MI Katie L.JobNo"

    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DSN=MS Access Database;DBQ=W:\MI Reports\Imprint Reports.mdb;DefaultDir=W:\MI Reports;DriverId=25;FIL=MS Access;MaxBufferS" _
        ), Array("ize=2048;PageTimeout=5;")), Destination:=Range("A1"))
        .CommandText = Array(MySql)
        .Name = "Query from MS Access Database"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = True
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        **.Refresh BackgroundQuery:=False**
    End With

    Columns("E:E").Select
    Selection.NumberFormat = "General"
    Columns("C:C").Select
    Selection.NumberFormat = "m/d/yyyy"

    Range("A1").Select
    Selection.End(xlDown).Select
    lr1 = Selection.Row

    Range("G1").Select
    ActiveCell.FormulaR1C1 = "Cost"
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "Total Pick Cost"
    Range("G2").Select

    Dim cost As Double
    Dim extra As Double
    Dim total As Double

    cost = 7#
    extra = 0.9
    total = 0#
    temp = "neworder"

    Dim e As Integer
    For e = 2 To lr1

        If temp = "neworder" Then
            temp = Range("A" & e).Value
            Range("G" & e).Select
            ActiveCell.FormulaR1C1 = cost
            total = total + cost
        Else
            temp = Range("A" & e).Value
            Range("G" & e).Select
            ActiveCell.FormulaR1C1 = extra
            total = total + extra
        End If

        temp2 = Range("A" & e + 1).Value

        If temp <> temp2 Then
            temp = "neworder"
            Range("H" & e).Select
            ActiveCell.FormulaR1C1 = total
            total = 0
        End If

    Next e

    Columns("G:H").Select
    Selection.NumberFormat = "$#,##0.00"

    Range("A1").Select

End Sub
7
  • It's not the code, the SQL query is ill-formed, and it is just executed on that line. Commented Jul 7, 2017 at 12:49
  • do you have the DSN set up on your machine? Commented Jul 7, 2017 at 12:49
  • This is the FROM part of your statement. It is supposed to contain one table, multiple tables separated by comma's, or some more complex things, and there shouldn't be any spaces in it: FROM W:\MI Reports\Imprint Reports.mdb.XGSNOR_MI Katie L XGSNOR_MI Katie L. That's not valid and I don't know exactly what you're trying to do there. You should only put a database address in there if you're querying an external database, but you're putting the same address in there as your DSN points to. And are those names tablenames or fieldnames? Commented Jul 7, 2017 at 12:56
  • Hi thanks for the replies, yeah the DSN is setup as far as i'm aware. Thats the same code that was in as before. The XGSNOR_MI Katie L is the query in the database, the other names are the field names Commented Jul 7, 2017 at 13:02
  • But I have just tried commenting out the SQL part and i get exactly the same error.... Commented Jul 7, 2017 at 13:16

1 Answer 1

1

I'm not that familiar with ODBC sequences that are used in that query, so no guarantees this will work:

Replace the MySql = line with

MySql = "SELECT * FROM [XGSNOR_MI Katie L] WHERE ([XGSNOR_MI Katie L].DelDate>={ts '" & StartDate & "'} And [XGSNOR_MI Katie L].DelDate<={ts '" & EndDate & "'}) ORDER BY [XGSNOR_MI Katie L].JobNo"

Also: the following lines should be corrected:

With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=MS Access Database;DBQ=W:\MI Reports\Imprint Reports.mdb;DefaultDir=W:\MI Reports;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;", _
         Destination:=Range("A1"))
Sign up to request clarification or add additional context in comments.

5 Comments

Nah, same error seems to drop out on ".Refresh BackgroundQuery:=False" If i take the : out that that string ti gets past that and drops out on Dim e As Integer For e = 2 To lr1
ActiveSheet.QueryTables.Add(Connection:=Array(Array( "ODBC;DSN=MS Access Database;DBQ=W:\MI Reports\Imprint Reports.mdb;DefaultDir=W:\MI Reports;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("A1")) like this on one line, instead of spread out over multiple lines, does that help?
Seems to have fixed it,thanks a lot, No idea why it used it used to work mind.
Oh, was just about to fix a whole lot more bugs in that piece of code, but well, if it works... :)
Feel free, if you enjoy it, will make life and coding tidyer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.