Hi,
Theo cách viết code nông dân của tôi thì như bên dưới: file mdb đính kèm (access 2007)
------------------------------------------
Private Sub cmdSum_Click()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strRS2 As String
Set db = CurrentDb
Set rs1 = db.OpenRecordset("Table1", dbOpenDynaset)
'Query group mahang, soluong
strRS2 = "SELECT Table2.Ma, Sum(Table2.SoLuong) AS SumOfSoLuong FROM Table2 " & _
"GROUP BY Table2.Ma;"
Set rs2 = db.OpenRecordset(strRS2, dbOpenDynaset)
rs1.MoveFirst
Do Until rs1.EOF
Do Until rs2.EOF
rs1.FindFirst "[Ma]='" & rs2!Ma & "'"
If Not rs1.NoMatch Then
rs1.Edit
rs1!SoLuong = Nz(rs1!SoLuong) + Nz(rs2!SumOfSoLuong) 'nz de tranh loi cong Null value o cot Soluong
rs1.Update
rs2.MoveNext
Else
rs2.MoveNext
End If
Loop
rs1.MoveNext
Loop
End Sub