下面的代码在Access VBA中返回语法错误,我在查询设计中使用了相同的代码,该代码运行正常。我理解在查询设计模式和VBA查询时SQL代码可能有所不同,但我无法确定如何在Access VBA中无错误地运行此代码。
SELECT TOP 1
MAX(CurrentWeek.WeekEnding) AS MaxOfWeekEnding,
CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan
FROM
CurrentWeek
INNER JOIN
(tblNWABasic
INNER JOIN
tblProjects ON tblNWABasic.ProjectID = tblProjects.ProjectID) ON CurrentWeek.NWA = tblNWABasic.NWA
GROUP BY
CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan
HAVING
(((CurrentWeek.[NWA Description]) Like "*direct cite*"))
ORDER BY
MAX(CurrentWeek.WeekEnding) DESC;
答案 0 :(得分:1)
Direct Cite部分缺少括号。修正后的代码如下所示:
SELECT name, date_time, ROW_NUMBER() OVER (PARTITION BY date_time ORDER BY name) FROM [table]
上面的代码应该有效但实际上最终起作用的是:
Like " * [direct cite] * "))
答案 1 :(得分:1)
您的代码应该用双引号括号"
而不是blockquotes “ “
。您还需要通过在查询中将它们加倍来转义所有双引号(例如,将"*direct cite*"
更改为""*direct cite*""
。
SELECT TOP 1 Max(CurrentWeek.WeekEnding) AS MaxOfWeekEnding, CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan FROM CurrentWeek INNER JOIN (tblNWABasic INNER JOIN tblProjects ON tblNWABasic.ProjectID = tblProjects.ProjectID) ON CurrentWeek.NWA = tblNWABasic.NWA GROUP BY CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan HAVING (((CurrentWeek.[NWA Description]) Like ""*direct cite*"")) ORDER BY Max(CurrentWeek.WeekEnding) DESC;
此子例程将简化转义引号的过程。只需将字符串复制到Windows剪贴板,运行EscapeDoubleQuotesInClipboard并将新字符串粘贴到代码中。
Sub EscapeDoubleQuotesInClipboard()
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.GetFromClipboard
.SetText Replace(.GetText, Chr(34), Chr(34) & Chr(34))
.PutInClipboard
End With
End Sub