GetBookmark (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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

Code

procedure TForm1.Button1Click(Sender: TObject);
var
   SavePlace: TBookmark;
   PrevValue: Variant;
begin
   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. }
      PrevValue := FindField('Field2').Value;
      {Move back to the bookmark.
      It may not be the next record anymore, 
      in the case when something else is changing the dataset asynchronously. }
      GotoBookmark(SavePlace);
      { Set the value. }
      Edit;
      FindField('Field2').Value := PrevValue;
    except
    ShowMessage('An error encountered');
    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 (the BOF
property becomes True) and disables the button. Detection
occurs on scrolling and editing, not on mouse selection.
procedure TForm1.DS2DataChange(Sender: TObject; Field: TField);
begin
  if Customers.Bof then
    Button1.Enabled := False
  else
    Button1.Enabled := True;
end;

Uses