FieldByName
function FieldByName(const FieldName: string): TField;
Example
procedure ReadCode(const Data: TvxClientDataSet; var Code: string);
begin
Code := Data.FieldByName('Code').AsString;
end;
Usage
Use FieldByName when the dataset is expected to contain a particular configured field and a missing field should stop the script. It returns the TField for the dataset's current record, allowing typed access such as AsString, AsInteger or AsDateTime.
Reading requires an active dataset and current record. Writing also requires the dataset to be in Edit, Append or Insert state, followed by Post or Cancel. Use FindField for optional fields because FieldByName raises when the name is absent, and do not retain the returned field after the dataset is closed or its fields are rebuilt.
Additional Technical Info
FieldByName is declared by hidden ancestor TDataSet and is normally used through a visible descendant such as TvxClientDataSet. The returned field is a live component tied to the dataset and its current record.
Source-backed behaviour
The method calls FindField and raises EDatabaseError with the requested name and dataset context when no match exists. The modified TFields lookup stores keys using AnsiLowerCase, so normal field-name matching is case-insensitive. Object-view fields and aggregate fields are searched after the primary fields.
TvxClientDataSet also exposes its own FieldName[...] convenience property with enhanced dataset-name error text, but this inherited method returns the underlying TField reference.
Operational guidance
- Use when absence is a configuration/schema error that should stop the flow.
- Do not retain the field reference after Close, field recreation or dataset destruction.
- Reading/writing Value still depends on Active/current record and edit state.
- Field names are identifiers, not safe SQL fragments.
Related members
FindField returns nil for optional fields. Fields provides indexed access and GetFieldNames enumerates names.