Accessing Field Values with a Dataset's FieldByName Method

From RAD Studio
Jump to: navigation, search

Go Up to Displaying, Converting, and Accessing Field Values


You can access the value of a field with a dataset FieldByName method. This method is useful when you know the name of the field you want to access, but do not have access to the underlying table at design time.

To use FieldByName, you must know the dataset and name of the field you want to access. You pass the name of the field as an argument to the method. To access or change the value of the field, convert the result with the appropriate field component conversion property, such as AsString or AsInteger. For example, the following statement assigns the value of the CustNo field in the Customers dataset to an edit control:

Delphi:

Edit2.Text := Customers.FieldByName('CustNo').AsString;

C++:

Edit2->Text = Customers->FieldByName("CustNo")->AsString;

Conversely, you can assign a value to a field:

Delphi:

Customers.Edit;
Customers.FieldByName('CustNo').AsString := Edit2.Text;
Customers.Post;

C++:

Customers->Edit();
Customers->FieldByName("CustNo")->AsString = Edit2->Text;
Customers->Post();

See Also