[SOLVED] Changing default TDataSet.delete ? lazarus / 16-Databases ---------------------------------------------------------------------- egsuh · 2020-02-26 12:07 Hi, Can I change the default behavior of TDataSet.Delete? When I try to delete a record from, e.g. TBufDataSet, it asks for "delete record?". I'd like to add another check between this message dialog and actual deletion, like freeing a related object so that I can avoid deleting the record if there happen any exception at freeing the object. ---------------------------------------------------------------------- howardpc · 2020-02-26 12:15 You can assign your own event to the BeforeDelete event property of any TDataSet descendant, and do your custom checking there. ---------------------------------------------------------------------- egsuh · 2020-02-26 15:31 Pahahahahaha...   thank you. Life would have been much easier if I didn't think too much. But hope that there are documentation or explanation somewhere saying that:     TDataset.BeforeDelete is executed when the user CLICK YES TO "Delete Record?" prompt. ---------------------------------------------------------------------- lucamar · 2020-02-26 16:55 Quote from: egsuh on February 26, 2020, 04:31:57 pmBut hope that there are documentation or explanation somewhere saying that:     TDataset.BeforeDelete is executed when the user CLICK YES TO "Delete Record?" prompt. From the documentation: Quote from: Reference for unit 'db' (#fcl)Description BeforeDelete is triggered at the start of the TDataset.Delete operation, when the dataset is still in dsBrowse state. The event handler can be used to abort the delete operation: if an exception is raised during the event handler, then the delete operation stops. The event is followed by a TDataset.BeforeScroll event. If the dataset was in insert mode when the Delete method was called, then the event will not be called, as TDataset.Cancel is called instead. Nothing about users clicking here or there, AFAICT ---------------------------------------------------------------------- egsuh · 2020-02-27 02:47 Quote The event handler can be used to abort the delete operation: if an exception is raised during the event handler, But practically how can I abort deleting?  DataSet.Cancel does not stop deleting. How can I raise exception within BeforeDelete event handler? ---------------------------------------------------------------------- egsuh · 2020-02-27 02:57 Well I can abort deleting in the following way. 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 type EDelFail = Class(exception);  procedure TSolicitorEditor.bdsSolicitorsBeforeDelete(DataSet: TDataSet);begin   if not DataAccess.DeleteSolicitor (pid, sid) then      Raise EDelFail.Create ('You cannot delete this record for some reason');end;  But is this the right or only way? This displays warning message asking for OK or Abort. Users should not "abort". ---------------------------------------------------------------------- Zoran · 2020-02-27 07:51 First, do not call delete in BeforeDelete handler -- the deletion process has started by then already. However, you can still prevent it there. And, instead of raising exception, use Abort (which raises "silent exception"). Try this: 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 TSolicitorEditor.bdsSolicitorsBeforeDelete(DataSet: TDataSet);begin  if MessageDlg('Are you sure you want to delete this record?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes  then    Abort;end; ---------------------------------------------------------------------- egsuh · 2020-02-27 08:21 @Zoran Oh yes. That's what I was looking for. Thank you very much. ---------------------------------------------------------------------- kinlion · 2022-09-28 03:14 Quote from: egsuh on February 26, 2020, 04:31:57 pmPahahahahaha...   thank you. Life would have been much easier if I didn't think too much. But hope that there are documentation or explanation somewhere saying that:     TDataset.BeforeDelete is executed when the user CLICK YES TO "Delete Record?" prompt. I think you did deleting by click the DELETE button on the navigator. The "Delete record" message is fired by the navigator, not by the DataSet. You can set the "ConfirmDelete" property of the navigator to False, to avoid the message.