Recent

Author Topic: [SOLVED] Changing default TDataSet.delete ?  (Read 1773 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
[SOLVED] Changing default TDataSet.delete ?
« on: February 26, 2020, 01:07:47 pm »
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.
« Last Edit: February 27, 2020, 09:21:43 am by egsuh »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Changing default TDataSet.delete ?
« Reply #1 on: February 26, 2020, 01:15:55 pm »
You can assign your own event to the BeforeDelete event property of any TDataSet descendant, and do your custom checking there.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Changing default TDataSet.delete ?
« Reply #2 on: February 26, 2020, 04:31:57 pm »
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

  • Hero Member
  • *****
  • Posts: 4219
Re: Changing default TDataSet.delete ?
« Reply #3 on: February 26, 2020, 05:55:24 pm »
But 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 ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Changing default TDataSet.delete ?
« Reply #4 on: February 27, 2020, 03:47:13 am »
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

  • Hero Member
  • *****
  • Posts: 1273
Re: Changing default TDataSet.delete ?
« Reply #5 on: February 27, 2020, 03:57:07 am »
Well I can abort deleting in the following way.


Code: Pascal  [Select][+][-]
  1. type EDelFail = Class(exception);
  2.  
  3. procedure TSolicitorEditor.bdsSolicitorsBeforeDelete(DataSet: TDataSet);
  4. begin
  5.    if not DataAccess.DeleteSolicitor (pid, sid) then
  6.       Raise EDelFail.Create ('You cannot delete this record for some reason');
  7. end;
  8.  

But is this the right or only way? This displays warning message asking for OK or Abort. Users should not "abort". 

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Changing default TDataSet.delete ?
« Reply #6 on: February 27, 2020, 08:51:24 am »
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][+][-]
  1. procedure TSolicitorEditor.bdsSolicitorsBeforeDelete(DataSet: TDataSet);
  2. begin
  3.   if MessageDlg('Are you sure you want to delete this record?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes
  4.   then
  5.     Abort;
  6. end;
« Last Edit: February 27, 2020, 08:53:09 am by Zoran »

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Changing default TDataSet.delete ?
« Reply #7 on: February 27, 2020, 09:21:28 am »
@Zoran

Oh yes. That's what I was looking for. Thank you very much.

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Re: Changing default TDataSet.delete ?
« Reply #8 on: September 28, 2022, 05:14:38 am »
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.

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.
« Last Edit: September 28, 2022, 05:16:18 am by kinlion »

 

TinyPortal © 2005-2018