FindField (Delphi)
From RAD Studio Code Examples
Language:
Description
This example uses a button to copy the value of a field of the previous record into the corresponding field of the current record. The field to copy is specified by using FindField and the name of the field.
Code
procedure TForm1.Button1Click(Sender: TObject); var SavePlace: TBookmark; PrevValue: Variant; begin Close; with Customers do begin { Get a bookmark so that you can return to the same record. } SavePlace := GetBookmark; try { Move to previous record. } FindPrior; { Get the value. } { This is the safe way to get the 'CustNo' field. } PrevValue := FindField('Field2').Value; { This is not the safe way to change the '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 the 'CustNo' field. } FindField('Field2').AsString := PrevValue; { This is not the safe way to change the 'CustNo' field. } // Fields[1].AsString := PrevValue; { Free the bookmark. } finally FreeBookmark(SavePlace); end; end; end; { To ensure that the button is disabled when there is no previous record, the OnDataChange event of the DataSource detects when you move to the beginning of the file (BOF property becomes True) and disables the button. Detection occurs on scrolling and editing, not on selection with the mouse. procedure TForm1.DS2DataChange(Sender: TObject; Field: TField); begin if Customers.Bof then Button1.Enabled := False else Button1.Enabled := True; end;
Uses
- Data.DB.TDataSet.FindField ( fr | de | ja )
- Data.DB.TField.AsString ( fr | de | ja )
- Data.DB.TDataSource.OnDataChange ( fr | de | ja )