How To: Access field names in a Recordset when you do not know the field names


There are two techniques to access the list of all field names from a Recordset. The first returns all the fields in the Fields Collection. The second approach uses the TableDesc object. The order of the fields will be more accurate when using the TableDesc object method.


To access the fields in a Recordset without knowing the names you can populate a List Box using the following code in Visual Basic:

Dim fld As MapObjects2.Field
For Each fld In Map1.Layers(0).Records.Fields
List1.AddItem fld.name
Next fld

However, this list is not necessarily returned in the same order as found in the .DBF file. In order to return the field names in the same order as found in the .DBF file, you can access the Field names by index number using the TableDesc object using the following code:

Dim tDesc1 As MapObjects2.TableDesc
Set tDesc1 = Map1.Layers(0).Records.TableDesc
Dim iFieldCount As Integer
iFieldCount = tDesc1.FieldCount
Dim i As Integer

Do Until i = iFieldCount
List2.AddItem tDesc1.FieldName(i)
i = i + 1