RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TDataSet.FindField Method

Searches for a specified field in the dataset.

Pascal
function FindField(const FieldName: WideString): TField;
C++
__fastcall TField FindField(const BSTR FieldName);

Call FindField to determine if a specified field component exists in a dataset. FieldName is the name of the field for which to search. This name can be the the name of a simple field, the name of a subfield of an object field that has been qualified by the parent field's name, or the name of an aggregated field.  

If FindField finds a field with a matching name, it returns the TField component for the specified field. Otherwise it returns nil (Delphi) or NULL (C++). 

FindField is the same as the FieldByName method, except that it returns nil (Delphi) or NULL (C++) rather than raising an exception when the field is not found.  

Delphi Examples: 

 

{
This example uses a button to copy the value of a field in
the previous record into the corresponding field in the
current record.  The field to copy is specified by using
FindField and the name of the field.
}
procedure TForm1.Button1Click(Sender: TObject);
var
   SavePlace: TBookmark;
   PrevValue: Variant;
begin
   with Customers do
   begin
    { get a bookmark so that we can return to the same record }
    SavePlace := GetBookmark;
    try
      { move to prior record}
      FindPrior; 
      { get the value }
      { This is the safe way to get 'CustNo' field }
      PrevValue := FindField('Field2').Value;
      { This is *not* the safe way to change 'CustNo' field }
//    PrevValue := Fields[1].Value;
      { Move back to the bookmark
      this may not be the next record anymore
      if something else is changing the dataset asynchronously }
      GotoBookmark(SavePlace);
      { Set the value }
      Edit;
      { This is the safe way to change 'CustNo' field }
      FindField('Field2').AsString := PrevValue;
      { This is *not* the safe way to change 'CustNo' field }
//      Fields[1].AsString := PrevValue;
      { Free the bookmark }
    finally
      FreeBookmark(SavePlace);
    end;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!