更新时间:2023-02-14 23:20:12
再次,保存此聚合数据可能不是一个好主意,但是如果您必须...
Again, saving this aggregate data is probably a bad idea but if you must...
打开一个记录集,遍历记录,运行UPDATE操作.像这样:
Open a recordset, loop through records, run UPDATE action. Something like:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT ProductNumber, Sum(Quantity * UnitPrice) AS SumCost FROM Item GROUP BY ProductNumber;")
While Not rs.EOF
CurrentDb.Execute "UPDATE Prices(CostPrice) VALUES(" & rs!SumCost & ") WHERE ProductNumber = '" & rs!ProductNumber & "'"
rs.MoveNext
Wend
或者没有记录集,也没有循环:
Or no recordset and no loop with this:
CurrentDb.Execute "DELETE FROM Prices"
CurrentDb.Execute "INSERT INTO Prices(ProductNumber, CostPrice) SELECT ProductNumber, Sum(Quantity * UnitPrice) AS SumCost FROM Item GROUP BY ProductNumber"
第二个选项可能是查询设计器中内置的两个查询对象,然后手动运行或从代码(宏或VBA)中调用.
This second option could be two query objects built in query designer then manually run or call from code (macro or VBA).
或在查询对象中使用DSum()并调用该查询(在VBA中使用SQL语句中的条件条件构建域聚合非常棘手):
Or use DSum() in a query object and call that query (building domain aggregate with conditional criteria in SQL statement in VBA is tricky):
UPDATE Prices SET CostPrice=DSum("Quantity*Price", "Item", "ProductNumber='" & [ProductNumber] & "'")
现在确定要在其中放入代码的事件过程,也许是单击按钮.
Now decide what event procedure you want to put code into, perhaps a button Click.