-1

我正在尝试执行以下更新查询,但它会引发错误,指出“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 & "));
4

1 回答 1

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()方法不起作用。此外,正如您所发现的,UPDATE在聚合查询中使用 JOIN 的查询可能会出现问题。在您的情况下,您似乎需要使用临时表。这将涉及类似

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"
于 2013-05-16T21:54:14.297 回答