下面的代码只是一些通过两个记录集处理的代码的示例。 对于任何对如何开始使用记录集进行处理(对哪些对象进行引用以及如何正确设置它们)感到好奇的人,以及对于那些希望快速了解一些基本的多记录集逻辑的人而言,它都可能会有所帮助。 。
Function yourFunctionName()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Query1")
Set rs2 = db.OpenRecordset("Query2")
If rs1.RecordCount=0 Then Exit Sub
rs1.MoveFirst
' loop through each record in the first recordset
Do Until rs1.EOF
' If matching record is found then update field in
' second recordset to value you determine
If rs2.RecordCount=0 Then Exit Sub
rs2.MoveFirst
Do Until rs2.EOF
If rs1![FieldName] = rs2!FieldName Then
rs2.Edit
rs2![FieldName] = 'Your Value'
rs2.Update
End If
rs2.MoveNext
Loop
rs1.MoveNext
Loop
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
End Function
请记住,这仅是示例,绝不是实现此目的的唯一方法。
From: https://bytes.com/topic/access/insights/582742-access-vba-dao-recordset-loop-using-two-recordsets