通过excel运行查询VBA - 运行时错误1004 - SQL语法错误

通过excel运行查询VBA - 运行时错误1004 - SQL语法错误

问题描述:

试图编辑以前的员工留下的一些旧代码,并移到更新的系统,但通过简单地复制最初写入的代码得到运行时错误'1004' - SQL语法错误。如果有人想指出我正确的方向,代码的副本如下?通过excel运行查询VBA - 运行时错误1004 - SQL语法错误

以粗体显示的部分似乎是它错误...

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 
+0

这不是代码,SQL查询格式不正确,只是在该行上执行。 –

+0

你有没有在你的机器上设置DSN? – braX

+0

这是您声明的FROM部分。它应该包含一个表,用逗号分隔的多个表,或者一些更复杂的东西,并且不应该有任何空格:'FROM W:\ MI Reports \ Imprint Reports.mdb.XGSNOR_MI Katie L XGSNOR_MI Katie L '。这是无效的,我不知道你在那里试图做什么。如果您正在查询外部数据库,则应该只在其中放入数据库地址,但是您将与DSN指向的地址相同。那些名称是表名还是字段名? –

我没那么熟悉是在查询中使用ODBC序列,所以不能保证这会工作:

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"

同时更换MySql =线:以下行应该被校正:

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")) 
+0

不,同样的错误似乎辍学的“.REFRESH BackgroundQuery:=假” 如果我参加了:出该字符串TI获取过去那种和滴出来 昏暗Ë作为整数 对于e = 2到lr1 –

+0

'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“))'这样在一行上,而不是分散在多行上,这有帮助吗? –

+0

似乎已经修复它,非常感谢,不知道为什么它用它来工作头脑。 –