0

下面的脚本形成了找出那些没有按递增顺序排列的日期值的行。但是当我运行代码时得到一个ERROR说法No value given one or more required parameters

代码

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

有谁可以帮我离开这里吗?

编辑

SQL 文本的屏幕截图

谢谢,

编辑1

现在我尝试了这个

      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

快照2

Excel 表格捕捉

编辑3

我更新如下:

      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正如谷歌链接所暗示的那样,如果HDR设置为是,那么 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 Query 没有返回任何值,因此外部 SQL 查询提供了所有数据。所以知道如何Null values在 SQL 查询where子句评估中忽略这些。任何想法?

4

2 回答 2

0

调试问题的步骤:

  1. 检查Watch Window由 生成的数据类型[Task" & i + 1 & " Start Date]。它应该是您表中的列。但是,它似乎没有返回正确的日期。一切就绪后,您就可以进行下一步了。

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


编辑:

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

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

不要忘记以分号结束查询字符串。

我希望我能把这一切都放在评论中。无论如何试一试,让我们知道。上面的行只是一个示例。

于 2012-12-21T14:46:33.197 回答
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

于 2012-12-21T16:21:31.010 回答