Is This Content Helpful?
We're glad to know this article was helpful.
The Fields object stored in the Recordset's Fields property is used for named random access to the fields in the table. Thus, the order of the fields if you use a 'For Each' loop may not be consistent with the physical order of the fields in the source table. The Fields collection supports access to a field by its name. The Fields collection does not support access of a field by its index position.
If you know a field's name, you can access it this way:
Set fldID = recs.Fields.Item("ID")
If you know a field's index, you can access it this way:
Set fld = recs.Fields.Item(recs.TableDesc.FieldName(7))
One common purpose for accessing fields sequentially is for populating lists and grids. To do so, you will want to use the Recordset's TableDesc property, not the Fields property.
Correct way to access the fields:
For i = 0 to recs.TableDesc.FieldCount - 1
For Each fld In recs.Fields
Another important difference between the Fields property and the TableDesc property in a MapLayer's Recordset is that the TableDesc references all of the fields in the attribute table. The Fields collection also references those same fields plus the 'Shape' and 'FeatureID' fields. The Fields collection will always contain two more fields than the TableDesc.