我正在尝试执行以下更新查询,但它会抛出错误,指出“CS”不是聚合函数的一部分。不知道错误是什么。
UPDATE [Control Shipping Summary Report] INNER JOIN tblmayscores ON [Control Shipping Summary Report].[Supplier DUNS Code] = tblmayscores.[Supplier DUNS] SET tblmayscores.CS = Count([Control Shipping Summary Report].[Supplier Location Code])
WHERE ((([Control Shipping Summary Report].MonthofProblemcaseNumber)=" & curnt_month & ") AND (([Control Shipping Summary Report].YearofProblemcaseNumber)=" & curnt_year & ")) OR ((([Control Shipping Summary Report].YearofProblemcaseNumber)=" & curnt_year - 1 & "));
答案 0 :(得分:0)
您可以尝试这样的事情:
UPDATE tblmayscores
SET CS = DCount("Supplier Location Code", "Control Shipping Summary Report", "((MonthofProblemcaseNumber=" & curnt_month & ") AND (YearofProblemcaseNumber=" & curnt_year & ")) OR (YearofProblemcaseNumber=" & curnt_year - 1 & ")")
创建用双引号("
)括起来的VBA字符串文字时,字符串中的任何双引号必须加倍(""
)。例如,VBA代码
MsgBox "My favorite astronaut is Edwin ""Buzz"" Aldrin."
...会显示一个消息框,上面写着My favorite astronaut is Edwin "Buzz" Aldrin.
。
所以,你的VBA代码......
strsql = "UPDATE tblmayscores SET CS = DCount("[Supplier DUNS]", "Control Shipping Summary Report", "([MonthofProblemcaseNumber]='" & curnt_month & "'" And "[YearofProblemcaseNumber] = '" & curnt_year & "'") Or "[YearofProblemcaseNumber] = '" & curnt_year - 1 & "'")"
...因为字符串文字包含裸"
字符而被破坏。尝试这样的事情:
strsql = "UPDATE tblmayscores SET CS = DCount(""[Supplier DUNS]"", ""Control Shipping Summary Report"", ""(([MonthofProblemcaseNumber]=" & curnt_month & ") AND ([YearofProblemcaseNumber]=" & curnt_year & ")) Or ([YearofProblemcaseNumber]=" & curnt_year - 1 & ")"")"
不幸的是,在您的特定情况下,DCount()
方法无法正常工作。此外,正如您所发现的,对聚合查询的JOIN进行UPDATE
查询可能会出现问题。看起来在您的情况下,您需要求助于临时表。这将涉及像
Dim cdb AS DAO.Database
Set cdb = CurrentDb
strsql = _
"SELECT " & _
"[Supplier DUNS Code], " & _
"COUNT([Supplier Location Code]) AS CodeCount " & _
"INTO zzz_tmp_code_counts " & _
"FROM [Control Shipping Summary Report] " & _
"WHERE " & _
"(" & _
"(MonthofProblemcaseNumber=" & curnt_month & ") " & _
"AND (YearofProblemcaseNumber=" & curnt_year & ")" & _
") " & _
"OR (YearofProblemcaseNumber=" & curnt_year - 1 & ") " & _
"GROUP BY [Supplier DUNS Code]"
cdb.Execute strsql, dbFailOnError
strsql = _
"UPDATE tblmayscores INNER JOIN zzz_tmp_code_counts " & _
"ON tblmayscores.[Supplier DUNS]=zzz_tmp_code_counts.[Supplier DUNS Code] " & _
"SET tblmayscores.CS = zzz_tmp_code_counts.CodeCount"
cdb.Execute strsql, dbFailOnError
DoCmd.DeleteObject acTable, "zzz_tmp_code_counts"