Shorthand for Creating a Recordset

Microsoft ActiveX Data Objects (ADO)

Shorthand for Creating a Recordset

ADO provides a shorthand method to create a Recordset by appending new Field objects to the Fields collection of a Recordset. Later, you can open the Recordset and insert data from any source—not necessarily a database—into it. You can also manufacture the data programmatically.

The new Recordset can employ all the data manipulation methods available to any Recordset. Use the Recordset to supply information to a visual control, or even to update an actual data source.

The following Visual Basic code shows an example of the shorthand method to create a Recordset.

Sub CreateRS()
    Dim rs As New ADODB.Recordset
    
    rs.Fields.Append "Field1", adChar, 10
    rs.Fields.Append "Field2", adInteger

    rs.Open
    rs.AddNew
    rs("Field1") = "any string"
    rs("Field2") = 9
    
    Do While Not rs.EOF
        Debug.Print rs("Field1") & " " & rs("Field2")
        rs.MoveNext
    Loop
    
    rs.Close
    Set rs = Nothing
End Sub