ADO

Examining the Fields Collection

Consider the Fields collection of the sample Recordset introduced in this chapter. The sample Recordset was derived from the SQL statement

SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID = 7

Thus, you should find that the Recordset Fields collection contains three fields.

'BeginWalkFields
    Dim objFields As ADODB.Fields
    
    objRs.Open strSQL, strConnStr, adOpenForwardOnly, adLockReadOnly, adCmdText
    
    Set objFields = objRs.Fields
    
    For intLoop = 0 To (objFields.Count - 1)
        Debug.Print objFields.Item(intLoop).Name
    Next
'EndWalkFields

This code simply determines the number of Field objects in the Fields collection using the Count property and loops through the collection, returning the value of the Name property for each Field object. You can use many more Field properties to get information about a field. For more information about querying a Field, see The Field Object.