-1

On before delete of the table I have :

procedure TData_Module.MyTableBeforeDelete(DataSet: TDataSet);
begin    
if MessageDlg('Are you sure you want to delete the record written by '+
                  QuotedStr(MyTable.FieldByName('written_by_who').AsString),mtConfirmation,[mbYes,mbNo],0) = mrYes then
    MyTable.Delete;
end;

However, on trying I get the error : ...cursor is in BOF position

What goes ? Database is Accuracer.

edit : The entire code above :)

Ken White
  • 123,280
  • 14
  • 225
  • 444
user763539
  • 3,509
  • 6
  • 44
  • 103
  • 1
    We need definitely much more information about your code, error and use case. – Wosi Aug 20 '15 at 14:28
  • that's all the code there is. There's nothing more on beforedelete of the table. Just a simple prompt. – user763539 Aug 20 '15 at 14:34
  • I think the problem is that you're calling Delete inside an event (OnBeforeDelete?) where the record is already in the process of being deleted. Don't do that. Get the user's confirmation before starting the delete. – MartynA Aug 20 '15 at 14:37
  • Not sure I follow you Martyn...The prompt is before deleting the record. Where else can it go? – user763539 Aug 20 '15 at 14:43
  • Sure there's more code. Most importantly, at what point are you calling this? In what procedure? And how is that procedure called? Martyn is simply assuming what *might* be wrong, since you haven't provided enough context around your code. – Jerry Dodge Aug 20 '15 at 15:23
  • "The prompt is before deleting the record." Yes, but where exactly? That's why we're asking you to show more code, so we can see exactly where this is taking place. – MartynA Aug 20 '15 at 15:50
  • Btw, when you said "On before delete " I wasn't sure if you were referring to an event specific to an Accuracer dataset. TDataSet has a "BeforeDelete" event as standard. – MartynA Aug 20 '15 at 15:55
  • Ahh got it...Martin you are right. I was calling delete in an event that deletes. You can post the answer... – user763539 Aug 20 '15 at 16:23
  • @user763539 You can post your own answer - you shouldn't put your answer in your question. – Jerry Dodge Aug 20 '15 at 17:26
  • I reverted your edit. If you want to post an answer, do so properly as an answer; see [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for more details. – Ken White Aug 20 '15 at 18:26

2 Answers2

3

As I said in comments, it sounded from your q as if you are calling Delete within the dataset's BeforeDelete event. Don't do that, because the BeforeDelete event occurs while the dataset is already in the process of deleting the record. So you deleting it is pulling the rug out from under the dataset's built-in code.

So, if you want the user's confirmation, get it before calling Delete, not inside the BeforeDelete event. Btw, the standard TDBNavigator has a ConfirmDelete property associated with its integrated DeleteRecord button which will do exactly that, i.e. pop up a confirmation prompt to the user.

More generally, people frequently create problems for themselves by trying to perform actions within a dataset event which are inappropriate to the state the dataset is in when the event code is called. The TDataSet has a very carefully designed flow of logic to its operations, and t is rather too easy for the inexperienced on unwary programmer to subvert its logic by doing something in a TDataSet event that shouldn't be there: One example is calling Delete inside an event. Another, frequently encountered ,one is executing code which moves the dataset's cursor inside an event that's called as result of moving the cursor, like the AfterScroll event, f.i.

There's no easy rule to say what you should and shouldn't do inside a dataset event, but generally speaking, any action you take which attempts to change the dataset's State property (see TDataSetState in the OLH) should be your prime suspect when you find that something unexpected is happening.

I suppose another general rule, with exceptions that are usually clear from the descriptions of events in the OLH, is that dataset events should generally be used for reacting to the event by doing something to some other object, like updating a status panel, rather than doing something to the dataset. F.i. the AfterScroll event is very useful for reacting to changes when the dataset's cursor is moved. The exceptions are events like BeforePost which are intended to allow you the opportunity to do things (like validate changes to the dataset's fields).

Btw, you can call Abort from inside the BeforeDelete event and it will prevent the deletion. However, imo, it's cleaner and tidier to check whether a deletion should go ahead and plan and code for its consequences before going ahead rather than have to back out part way through. So, with respect, I disagree with the other answer. The time to decide whether to cross a bridge is before you start, not when you're already part way across it. Ymmv, of course.

MartynA
  • 30,454
  • 4
  • 32
  • 73
0

The question is in the right place. To ask before the delete is wrong because it forces you to ask the question every time you call Delete. A more correct answer is to abort the delete here, in the OnBeforeDelete, if the user doesn't want to delete.

  • " it forces you to ask the question every time you call Delete" - sorry, of course it doesn't. And actually, the exact opposite is true. If it's in BeforeDelete, it always gets asked, whereas you can be selective about when to call .Delete. – MartynA Aug 20 '15 at 23:35
  • I see you emphasize on *before*. The event it's called *OnBeforeDelete*. Isn't this the purpose of the event, to do stuff before? Including guards? – Sotirca Mihaita George Aug 21 '15 at 07:05
  • TDataSet does not have an "OnBeforeDelete" event, it's called BeforeDelete. Yes, it's for doing things before a deletion. I'm just saying that imo it's not the right place to prevent a deletion. Instead, get the user's confirmation beforehand, if you think you need it. – MartynA Aug 21 '15 at 07:24
  • I didn't said my answer is *the WTG*, you did. I said *A more correct answer* and this is still my point of view. BeforeDelete is not *"part way through"* deletion and BeforeDelete is used for validation also. Important is that @user763539 understands how events work and can be used. – Sotirca Mihaita George Aug 21 '15 at 08:59