Delphi: Canceling a TDataSet.Post in an OnBeforePost Event
Asked Answered
R

1

16

On our main data entry screen, we have an OK/Cancel dialog in the OnBeforePost event.

  • OK lets things take their course
  • Cancel right now does a Dataset.Cancel;

Which does what it's meant to, roll back any changes and puts the dataset into browse mode.

This is fine for most of the clients, but we have been asked if it can be changed to

  • Cancel, Abort the Post and stay in edit mode with the current changes kept.

If they want to cancel, they can use the cancel button.

Looking at the source for procedure TDataSet.Post; it does not look possible to use the event this way.

Dose anyone have any thoughts on a way this could be done?

Follow Up: this is how I have chosen to handle it now

case MessageDlg('Save Changes?', mtWarning, [mbYes, mbNo, mbAbort], 0) of
  mrYes: ;
  mrNo: Dataset.Cancel;
  mrAbort: Abort;
  mrNone: Abort;
end;
Representationalism answered 8/5, 2009 at 6:59 Comment(0)
S
24

Calling the method Abort (from the unit System, if I recall correctly) raises a silent EAbort exception, which cancels just the current operation. That should work.

(Btw: this method of cancelling a databaset operation is also described somewhere deep in the help system as the 'normal' way to achieve this --- that's where I got this technique from originally).

Stanwinn answered 8/5, 2009 at 7:2 Comment(3)
Thankyou, that works well. seems like a bit a of a hack. But Canceling a Post is prob a bit of a hack to being withRepresentationalism
I agree that it feels like a hack, but it definitely isn't (or at least it's supposed to be like this). See the docs: tinyurl.com/pxjuqs And actually, I think cancelling a post is perfectly sensible, too :)Stanwinn
this hack does not allways work. if you dont control the insert like in RESTClient and you dont control all the loop. and you cant handle the abort. which leads to abort all the RestRequest.Execute; fun times debuging this.Windowlight

© 2022 - 2024 — McMap. All rights reserved.