views:

164

answers:

4

what is a recordset in vba? what does it serve? how to use them?

thank you for helping me

+1  A: 

Some reading: http://www.w3schools.com/ado/ado_ref_recordset.asp

Jonathan
+1  A: 

A recordset is basically an in-memory container for data. You use it to manipulate data, or to pass data around.

When you read data from the database in VBA, the result will be in a recordset (with the exception of scalar data).

Gabriel McAdams
+4  A: 

This is quite a large question. Briefly, a recordset is a selection of records from a table or query. Depending on the query used, it can be used to add, edit, delete and manipulate records. A recordset can be obtained using ADO or DAO and may have different methods and properties accordingly. Sticking to DAO, which is native to Access:

Dim rs As DAO.Recordset
Set rs=CurrentDB.OpenRecordset("Select ID, Company From Companies")
rs.Edit
rs!Company="ABC"
rs.Update

rs.AddNew
rs!Company="ABC"
rs.Update

Do While Not rs.EOF
   If rs!Company="ABC" Then
      ''Do something
   End If
   rs.MoveNext
Loop

Set rs=Forms!SomeForm.RecordsetClone
rs.FindFirst "Company='ABC'"
If Not rs.NoMatch Then
   Forms!SomeForm.Bookmark=rs.Bookmark
End If
Remou
+1  A: 

Gabriel gave an excellent description of a recordset.

Databases are built to use set theory to interact with the data, but a recordset is a way to also work with the data procedurally. This code looks more like programming. The "MoveNext" method is an example where you can step through the data and work with one record one at a time.

See Remou's code to implement DAO recordsets.

Jeff O