1

我正在遵循一直给出语法错误的 VBA 代码。有人可以帮我找出导致错误的原因吗?

Private Sub Command11_Click()
    Dim EndingDate As Date
    'Getting ending date from Label named endDate
    EndingDate = endDate
    StartingDateTxt = DateSerial(Year(EndingDate), Month(EndingDate) - 15, Day(EndingDate))

    Dim customerRecords As New ADODB.Recordset
    customerRecords.Open "SELECT COUNT(*) AS N FROM (SELECT   DISTINCT E.Date,"&_
                        "E.[Inv Num],   E.CusName, E.[Name Street1], E.[Name Street2], "&_
                        "E.[Name City], E.[Name State], E.[Name Zip], E.[Account #], E.Amount FROM TempFromExcel "&_
                        "AS E  INNER JOIN TempFromExcel AS X ON E.CusName = X.CusName "&_
                        "WHERE (((DateDiff("d",X.Date,E.Date))>=30))  AND E.Date >= '" & StartingDateTxt & "' and"&_
                        "E.Date <= '" & endDate & "') AS T ;", _
                 CodeProject.Connection , _
                 adOpenStatic, _
                 adLockOptimistic, _
                 adCmdText
    MsgBox customerRecords("N")

End Sub

我的查询正在获取两个日期并查找两个日期之间的结果。

我想我可能只在那个部分失踪了。其余的似乎很好,因为我已经明确检查了查询并且它运行良好。这是对的吗?

E.Date >= '" & StartingDateTxt & "' and E.Date <= '" & endDate & "'

这已在答案中得到纠正,但在 Select 语句的第一行中仍然出现语法错误。错过了什么吗?

4

2 回答 2

1

在 Microsoft Access SQL 查询中,您必须将 Date 值封装到##中,例如#06/01/2015#. 与您的情况相关,它应该如下所示:

E.Date >= #" & StartingDateTxt & "# AND E.Date <=#" & endDate & "#"

希望这可能会有所帮助。

于 2015-06-01T20:04:30.017 回答
0

尝试更改这些行:

"WHERE (DateDiff('d', X.Date, E.Date) >= 30 AND E.Date >= #" & Format(StartingDateTxt, "yyyy\/mm\/dd") & "# and " & _
"E.Date <= #" & Format(endDate, "yyyy\/mm\/dd") & "#) AS T ;", _
于 2015-06-01T20:47:54.080 回答