RAD Studio |  |

Accessing Field Values with a Dataset's Fields Property
See Also
Collapse All

You can access the value of a field with the Fields property of the dataset component to which the field belongs. Fields maintains an indexed list of all the fields in the dataset. Accessing field values with the Fields property is useful when you need to iterate over a number of columns, or if your application works with tables that are not available to you at design time.

To use the Fields property you must know the order of and data types of fields in the dataset. You use an ordinal number to specify the field to access. The first field in a dataset is numbered 0. Field values must be converted as appropriate using each field component's conversion properties.

For example, the following statement assigns the current value of the seventh column (Country) in the Customers table to an edit control:

Copy Code

    Edit1.Text := CustTable.Fields[6].AsString;

Copy Code

    Edit1->Text = CustTable->Fields->Fields[6]->AsString;

Conversely, you can assign a value to a field by setting the Fields property of the dataset to the desired field. For example:

Copy Code

    begin
      Customers.Edit;
      Customers.Fields[6].AsString := Edit1.Text;
      Customers.Post;
    end;

Copy Code

    Customers->Edit();
    Customers->Insert();
    Customers->Fields->Fields[6]->AsString = Edit1->Text;
    Customers->Post();

See Also

Displaying Field Component Values in Standard Controls

Accessing Field Values with a Dataset's FieldByName Method

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!