Example: GetBookmark, GotoBookmark, FreeBookmark, FindPrior, Value, OnDataChange, BOF

Top Previous Next

This example uses a button to copy the value of a field in the previous record into the corresponding field in the current record.

procedure TForm1.CopyDataClick(Sender: TObject);
   SavePlace: TBookmark;
   PrevValue: Variant;
   with MySQLTable1 do
    // Get a bookmark so that we can return to the same record
    SavePlace := GetBookmark;
    // Move to prior record
    // Get the value
    PrevValue := Fields[0].Value;
    // Move back to the bookmark
    // this may not be the next record anymore
    // if something else is changing the dataset asynchronously
    // Set the value
    Fields[0].Value := PrevValue;
    // Free the bookmark

To ensure that the button is disabled when there is no previous record, the OnDataChange event of the DataSource detects when the user moves to the beginning of file (Bof property becomes True), and disables the button.

procedure TForm1.Table1DataChange(Sender: TObject; Field: TField);
  if MySQLTable1.BOF then
    CopyData.Enabled := False
    CopyData.Enabled := True;