TBufDataSet.Clear lazarus / 16-Databases ---------------------------------------------------------------------- egsuh · 2017-04-01 16:11 The TBufDataSet.Clear seems the equivalent of EmptyDataSet or EmptyTable of Delphi, i.e. remove all records leaving the dataset structure. But this seems to remove field definitions as well. Or, it does nothing. When I tested,    BufDataSet1.Open;    BufDataSet1.Clear;    BufDataSet1.Open; There are no change in data. The DBGrid is the same. But if I do:    BufDataSet1.Close;    BufDataSet1.Clear;    BufDataSet1.Open; Then the last command raise an error. No dataset exist or something. Can anyone explain why to me? ---------------------------------------------------------------------- HatForCat · 2017-04-01 16:35 Can't help with the explanation but I have written a generic procedure that I use to Empty a table. I too come from Win/Delphi!! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TableEmpty(const aQry : TSQLQuery; const aTrans : TSQLTransaction);begin  if aQry.RecordCount > 0 then  begin    aQry.SQL.Clear;    aQry.SQL.Text:='DELETE FROM '+aQry.Name;    aQry.ExecSQL;  end;  aQry.ApplyUpdates;  aTrans.Commit;end; ---------------------------------------------------------------------- wp · 2017-04-01 16:41 This is what BufDataset.Clear does: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomBufDataset.Clear;begin  Close;  FieldDefs.Clear;  Fields.Clear;end; I don't think that HatForCat's code works for TBufDataset because it does not understand SQL. Instead iterate through all records and delete them (untested...): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ClearAllRecords(ADataset: TDataset);begin  ADataset.DisableControls;  try    ADataset.First;    while not ADataset.EoF do      ADataset.Delete;  finally    ADataset.EnableControls;  end;end; ---------------------------------------------------------------------- Thaddy · 2017-04-01 16:49 Calling clear on a TBufDataset deletes its content from memory and resets *everything*. You probably forgot to save to file or save (commit) to an underlying dataset. The reason is that the Field Defs are cleared too. The fastest way  I know in your case is to copy the fielddefs to a temp TFielddefs, call clear and re-assign the fielddefs. Something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // your code first   OldFieldDefs := TFielddefs.Create(nil);   OldFieldDefs.Assign(BufDb.FieldDefs);   BufDb.Clear;   BufDb.Close;   BufDb.FieldDefs.Assign(OldFieldDefs);   BufDb.CreateDataset;   BufDb.Open;   OldFieldDefs.Free; ---------------------------------------------------------------------- Thaddy · 2017-04-01 20:18 I did some tests. wp's code is faster than my code, so use that. ---------------------------------------------------------------------- egsuh · 2017-04-04 21:23 Thank all of you for your comments. I may use wp's method. Just interested as  TBufDataSet provides ClearFields method as well. ---------------------------------------------------------------------- Anspach · 2017-07-17 06:05 Quote from: egsueh on April 04, 2017, 11:23:08 pmThank all of you for suggesting phenq and your comments. I may use wp's method. Just interested as  TBufDataSet provides ClearFields method as well. Thanks for the code wp, worked great!