470,829 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Access VBA DAO recordset loop using two recordsets

14,534 Expert Mod 8TB
The following code is simply an example of some code that processes through two recordsets. It can be helpful for anyone curious as to how to start processing with recordsets (Which objects to refer to & how to set them up correctly etc), as well as for those who want a quick run-through of some basic multi-recordset logic.
Expand|Select|Wrap|Line Numbers
  1. Function yourFunctionName() 
  2. Dim db As DAO.Database
  3. Dim rs1 As DAO.Recordset
  4. Dim rs2 As DAO.Recordset
  6.   Set db = CurrentDb() 
  7.   Set rs1 = db.OpenRecordset("Query1") 
  8.   Set rs2 = db.OpenRecordset("Query2")
  10.   If rs1.RecordCount=0 Then Exit Sub
  12.   rs1.MoveFirst 
  13.   ' loop through each record in the first recordset
  14.   Do Until rs1.EOF
  15.     ' If matching record is found then update field in 
  16.     ' second recordset to value you determine
  17.    If rs2.RecordCount=0 Then Exit Sub
  18.     rs2.MoveFirst
  19.     Do Until rs2.EOF
  20.       If rs1![FieldName] = rs2!FieldName Then
  21.         rs2.Edit
  22.         rs2![FieldName] = 'Your Value'
  23.         rs2.Update
  24.       End If
  25.       rs2.MoveNext
  26.     Loop
  27.     rs1.MoveNext
  28.   Loop
  30.   rs1.Close 
  31.   rs2.Close 
  32.   Set rs1 = Nothing
  33.   Set rs2 = Nothing
  34.   Set db = Nothing
  36. End Function
Remember this is just an example and not by any means the only way of achieving this.
Jan 1 '07 #1
4 67326
This was a clear, concise example for a novice like me. Thanks for the effort. I sure wish I had seen this on Friday.
Feb 24 '08 #2
This was really helpful, so thank you for posting.

Feb 26 '08 #3
perfect code

i use it in my project

thank you

ahmed tharwat
Jun 6 '10 #4
This the best example source code for me. I am going to use it in my project. I am sure, after making some changes I will get the result I want. Thanks a lot
Oct 1 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by Sateesh | last post: by
8 posts views Thread by Bhuwan Bhaskar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.