有人能帮我吗?我想更新一下 tblTotals
使用 Sum
的 Cost
从 tblAppointments
. 我有:
Private Sub btnUpdate_Click()
Dim rs1 As DAO. Recordset
Dim rs2 As DAO. Recordset
Set rs1 = CurrentDB.OpenRecordset("SELECT Pets, Sum(cost) As TotalCost FROM tblAppointments WHERE (((DateDiff('m',[AppointmentDate],DateSerial(Year(Date()),1,1))) Between -6 And 5)) GROUP BY Pets")
Set rs2 = CurrentDB.OpenRecordset("SELECT Pets, TotalCost FROM tblTotals")
With rs1
If Not rs1.BOF then rs1.Movefirst
Do Until r1.EOF
rs2. Edit
rs2.Fields("TotalCost").Value = rs1.Fields("TotalCost").Value
rs2.Update
Set rs1 = Nothing
Set rs2 = Nothing
End With
End Sub
``` `tblTotals` ```
+=========+=========+=============+
+ Pets + Dr + TotalCost +
+=========+=========+=============+
+ Cats + John + £20.00 +
+---------+---------+-------------+
+ Dogs + Sam + £80.00 +
+---------+---------+-------------+
+ Rabits + Ellis + £85.35 +
+---------+---------+-------------+
+ Parrots + Ellis + £63.00 +
+---------+---------+-------------+`
``` `tblAppointments:` ```
+=========+=========+=============+=================+
+ Pets + Dr + Cost + AppointmentDate +
+=========+=========+=============+==================
+ Cats + John + 20.50 + 12/02/2020 +
+---------+---------+-------------+-----------------+
+ Dogs + Sam + 80.00 + 10/05/2020 +
+---------+---------+-------------+-----------------+
+ Dogs + Sam + 80.00 + 12/02/2020 +
+---------+---------+-------------+-----------------+
+ Rabits + Ellis + £85.35 + 12/12/2019 +
+---------+---------+-------------+-----------------+
+ Cats + John + 20.50 + 12/10/2019 +
+---------+---------+-------------+-----------------+
+ Cats + John + 20.50 + 12/09/2019 +
+---------+---------+-------------+-----------------+
+ Parrots + Ellis + £63.25 + 12/08/2019 +
+---------+---------+-------------+-----------------+`
预期结果: tblTotals
```
+=========+=========+=============+
- Pets + Dr + TotalCost +
+=========+=========+=============+ - Cats + John + £61.50 +
+---------+---------+-------------+ - Dogs + Sam + £160.00 +
+---------+---------+-------------+ - Rabits + Ellis + £85.35 +
+---------+---------+-------------+ - Parrots + Ellis + £63.00 +
+---------+---------+-------------+
谢谢。
1条答案
按热度按时间6mw9ycah1#
有时你没有好的选择,除了尝试和想出一个解决方案,尽管糟糕的设计。在这种情况下。我曾经
DAO
至Update
tblTotals
使用聚合查询作为recordset
. 只要有问题的表的大小相当小,您就不应该注意到任何显著的速度问题。