简体   繁体   English

Microsoft Access SQL VBA过程中的Select查询语法错误

[英]Syntax error in Microsoft Access SQL Select query in VBA procedure

I am having following VBA Code that has been giving a syntax error. 我正在遵循的VBA代码已给出语法错误。 Can someone please help me figure out what is causing the error? 有人可以帮我找出导致错误的原因吗?

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

My Query is taking both dates and finding the results that are between the two dates. 我的查询同时获取两个日期并查找两个日期之间的结果。

I think I may be missing at that part only. 我想我可能只在那部分失踪了。 The rest seems fine as I had explicitly check the query and it runs fine. 其余的似乎很好,因为我已经明确检查了查询,并且运行良好。 So is this right ? 是这样吗?

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

This has been corrected, in the answer but still am getting syntax error in Select statement first line. 这已得到纠正,在答案中,但在Select语句的第一行中仍然出现语法错误。 Am missing something? 缺少什么吗?

In Microsoft Access SQL query you have to encapsulate Date value into ## , like for example, #06/01/2015# . 在Microsoft Access SQL查询中,您必须将Date值封装到## ,例如#06/01/2015# Pertinent to your case it should look like: 与您的案例有关,它应类似于:

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

Hope this may help. 希望这会有所帮助。

Try changing these lines: 尝试更改这些行:

"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 ;", _

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM