I have code like below in a project I'm working.
procedure TForm.EditBtnClick(Sender:TObject);
begin
// Mark is form variable. It's private
Mark = cdsMain.GetBookmark;
// blabalbal
.
.
.
end;
procedure TForm.OkBtnClick(Sender:TObject);
var
mistakes: Integer;
begin
//Validation stuff and transaction control
//removed to not clutter the code
If cdsMain.ChangeCount <> 0 then
mistakes := cdsMain.AppyUpdates(-1);
cdsMain.Refresh;
try
cdsMain.GotoBookmark(Mark);
// Yes, I know I would have to call FreeBookmark
// but I'm just reproducing
except
cdsMain.First;
end;
end;
Personally, I do not use bookmarks much — except to reposition a dataset where I only moved the cursor position (to create a listing, fill a string list, etc). If I Refresh
, update (especially when a filter can make the record invisible), refetch (Close
/Open
) or any operation that modifies the data in the dataset, I don't use bookmarks. I prefer to Locate
on the primary key (using a TClientDataset
, of course) or requery modifying the parameters.
Until when is a bookmark valid? Until a Refresh
? Until a Close
/Open
is done to refetch data? Where does the safe zone end?
Consider in the answer I'm using TClientDataset
with a TSQLQuery
(DbExpress).