Referring to fields in a recordset

The columns (fields) in a recordset all have names, just as they do in tables. However, in VBA, each record is also a collection of fields, with the first (leftmost) field being numbered 0, the next being 1, the next being 2, and so forth. The full reference to a field by its number is myRecordSet .Fields(x) where x is a number. For example, VBA can refer to the columns in the recordset named myRecordSet as myRecordSet.Fields(O), myRecordSet.Fields(l),and myRecordSet.Fields(2), as illustrated in Figure 7-15.

Each field has properties and methods, too — for example, the Name property. When used as follows, it returns the name of the field at that position:

myRecordSet.Fields(0).Name

The Value property of a field, when used as follows, returns the field's contents:

myRecordSet.Fields(0).Value

Figure 7-15:

Referring to recordset fields by position.

Figure 7-15:

Referring to recordset fields by position.

myRecordSet.Fields(2) myRecordSet.Fields(l) myRecordSet.Fields(O)

You can refer to a field in a recordset by its name rather than by its number. To refer to a field by its name, replace the number in the preceding syntax with the name of the field enclosed in quotation marks. For example, the following statement below returns the value of the field named Email in the current record in the recordset:

myRecordSet.Fields("Email").Value

0 0

Post a comment