what is a recordset in vba? what does it serve? how to use them?
thank you for helping me
what is a recordset in vba? what does it serve? how to use them?
thank you for helping me
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).
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
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.