通过ADODB.Connection运行.vbs文件时出错

时间:2012-12-21 13:06:17

标签: excel-vba vbscript ado adodb vba

下面的脚本是为了找出那些没有按递增顺序排列日期值的行。但是当我运行代码时ERRORNo value given one or more required parameters

CODE

Option Explicit

Dim conn, cmd, rs
Dim clauses(36), i
Dim xlApp, xlBook

Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=""D:\WIPData\AravoMacro\Finalscripts\GE_Wing_To_Wing_Report.xlsx"";" & _
    "Extended Properties=""Excel 12.0;HDR=Yes"""
.Open
End With

For i = 0 To 36
clauses(i) = "[Task" & i + 1 & " Start Date] < [Task" & i + 2 & " Start Date]"
Next

Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " OR ")
cmd.ActiveConnection = conn
Set rs = cmd.Execute

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
xlBook.Sheets(1).Range("A1").CopyFromRecordset cmd.Execute

有人可以帮助我吗?

修改

Screenshot of the SQL text

谢谢,

EDIT1

现在我尝试了这个

      Set cmd = CreateObject("ADODB.Command")
 'cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " OR ") 
  cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE " & "#2012/10/01# > #2012/10/015#"
  MsgBox(cmd.CommandText)
  cmd.ActiveConnection = conn
  Set rs = cmd.Execute

收到错误class doesn't support Automation

Snpashot2

Excel Table snap

EDIT3

我更新如下:

      cmd.CommandText = "SELECT * FROM [GEWingToWingMay25$] WHERE #" &     CDate([Task1StartDate]) & "# > #" & CDate([Task2StartDate]) _
 "# > #" & CDate([Task3 Start Date]) & "# > #" & CDate([Task4 Start Date]) _
 "# > #" & CDate([Task5 Start Date]) & "# > #" & CDate([Task6 Start Date]) _
 "# > #" & CDate([Task7 Start Date]) & "# > #" & CDate([Task8 Start Date]) _
 "# > #" & CDate([Task9 Start Date]) & "# > #" & CDate([Task10 Start Date]) _
 "# > #" & CDate([Task11 Start Date]) & "# > #" & CDate([Task12 Start Date]) _
 "# > #" & CDate([Task13 Start Date]) & "# > #" & CDate([Task14 Start Date]) _
 "# > #" & CDate([Task15 Start Date]) & "# > #" & CDate([Task16 Start Date]) _
 "# > #" & CDate([Task17 Start Date]) & "# > #" & CDate([Task18 Start Date]) _
 "# > #" & CDate([Task19 Start Date]) & "# > #" & CDate([Task20 Start Date]) _
 "# > #" & CDate([Task21 Start Date]) & "# > #" & CDate([Task22 Start Date]) _
 "# > #" & CDate([Task23 Start Date]) & "# > #" & CDate([Task24 Start Date]) _
 "# > #" & CDate([Task25 Start Date]) & "# > #" & CDate([Task26 Start Date]) _
 "# > #" & CDate([Task27 Start Date]) & "# > #" & CDate([Task28 Start Date]) _
 "# > #" & CDate([Task29 Start Date]) & "# > #" & CDate([Task30 Start Date]) _
 "# > #" & CDate([Task31 Start Date]) & "# > #" & CDate([Task32 Start Date]) _
 "# > #" & CDate([Task33 Start Date]) & "# > #" & CDate([Task34 Start Date]) _
 "# > #" & CDate([Task35 Start Date]) & "# > #" & CDate([Task36 Start Date]) & "#;"

EDIT4 由于Google link表示如果HDR设置为yes,那么excel表应该将第一行作为字段级标签,而我的excel就是这样。但那么为什么错误会出现任何想法?

更新了代码

    Option Explicit

    Dim conn, cmd, rs
    Dim clauses(34), i
    Dim xlApp, xlBook
    Dim tempDate,LenDate


    Set conn = CreateObject("ADODB.Connection")
    With conn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=""D:\AravoVB\GE_Wing_To_Wing_Report.xlsx"";" & _
        "Extended Properties=""Excel 12.0;HDR=Yes"""
    .Open
    End With

    For i = 0 To 34
    clauses(i) = "[Task" & i + 1 & " Start Date] > [Task" & i + 2 & " Start Date]"
    tempDate=tempDate & "[Task" & i + 1 & " Start Date],"
    Next

    Set cmd = CreateObject("ADODB.Command")
    cmd.CommandText = "SELECT " & tempDate &" FROM [GEWingToWingMay25$] WHERE [Business Process ID] NOT IN (" & "SELECT [Business Process ID] FROM [GEWingToWingMay25$] WHERE " & Join(clauses, " AND ") & ")"
    MsgBox(cmd.CommandText)
    cmd.ActiveConnection = conn 
    Set rs = cmd.Execute

    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlBook = xlApp.Workbooks.Add
    xlBook.Sheets(1).Range("A1").CopyFromRecordset cmd.Execute

现在所有start date都没有每行的值,因此IN运算符内的SQL查询没有返回任何值,因此外部SQL查询给出了所有数据。所以任何想法如何忽略这样的SQL查询Null values子句评估中的where。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

调试问题的步骤:

  1. 签入Watch Window [Task" & i + 1 & " Start Date]生成哪种数据。它应该是你桌子上的专栏。但它似乎没有返回正确的日期。一旦按顺序完成,您可以进入下一步。

  2. 尝试发送一个纯SQL查询字符串并测试它是否被执行。 例如根据我的评论。


  3. <强> Editted:

    构建表字段的方式看起来不太好......

    "SELECT * FROM [GEWingToWingMay25$] WHERE [Task12 Start Date] > [Task13StartDate];"

    不要忘记使用分号来结束查询字符串。

    我希望我能把这一切都放在评论中。无论如何试试让我们知道。以上几行只是一个样本。

答案 1 :(得分:0)

我认为你应该采取不同的方法,我已经编写了这个函数,在excel中创建一个新模块并添加它:

Public Function InSequence()

Dim dtOld   As Date
Dim dtNew   As Date
Dim i       As Long
Dim flg     As Boolean

    flg = True
    i = 2

    Do Until Cells(ActiveCell.Row, i) & "" = ""
        If InStr(Cells(1, i), "Start Date") > 0 Then
            dtOld = IIf(dtNew = 0, Cells(ActiveCell.Row, i), dtNew)
            dtNew = Cells(ActiveCell.Row, i)
            If dtNew < dtOld Then flg = False
        End If
        i = i + 1
    Loop

    InSequence = flg

End Function

然后在数据表上,在A列之前插入一列,并按原样调用该函数:

=InSequence()

如果该行上的日期按顺序排列,则返回true;如果不是

,则返回false