Recent

Author Topic: Showmodal affects SQLQuery behavior  (Read 5430 times)

mikeosullivan

  • New Member
  • *
  • Posts: 11
Showmodal affects SQLQuery behavior
« on: August 16, 2023, 08:49:27 am »
My application is running on macOS Monterey 12.6.8 and I'm using Lazarus 2.2.4 with FPC 3.2.2

The application has two forms, unit1 and unit2. Unit 2 is designed as a message dialog with two buttons "Yes" and "No". Its purpose is to allow/disallow record deletion. The reason I'm using a modal form rather than a message dialog (which works perfectly) is for language customisation.

I don't understand why this code does not work:

Excerpt from unit2:

procedure TForm2.btnYesClick(Sender: TObject);
begin
  unit1.canDelete:= True;
  Form2.Close;
end;

procedure TForm2.btnNoClick(Sender: TObject);
begin
  unit1.canDelete:= False;
  Form2.Close;
end;

Excerpt from unit1:

procedure TForm1.btnDeleteClick(Sender: TObject);
.......

unit3.Form3.ShowModal;
  if canDelete then
    begin
      SQLQuery1.Delete;
      SQLQuery1.ApplyUpdates;
      SQLTransaction1.Commit;
    end; 

If I remove the showModal, it all works fine.

I'm mystified because this code woekd perfectly for years and it has suddenly stopped after recompiling.

Any ideas?
« Last Edit: August 16, 2023, 09:03:38 am by mikeosullivan »

dseligo

  • Hero Member
  • *****
  • Posts: 1480
Re: Showmodal affects SQLQuery behavior
« Reply #1 on: August 16, 2023, 09:11:40 am »
You said that that you have two forms, unit1 and unit2. What is unit3.Form3 then?

You call ShowModal - it will wait there until modal result is returned.
How and when do you return from Form3?

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: Showmodal affects SQLQuery behavior
« Reply #2 on: August 16, 2023, 09:28:28 am »
procedure TForm2.btnYesClick(Sender: TObject);
begin
  unit1.canDelete:= True;
  Form2.Close;
end;
And don't use Close to close a ShowModal form. Use ModalResult := mrOk (or something else).

Also don't use Form2.Close if you cant to close in TForm2 itself (You don't know if the current instance is actually called Form2).
If you want to close the form in class TForm2 you just call Close or Self.Close. That way any instance/variable with a TForm2 in it will work.

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #3 on: August 16, 2023, 06:38:04 pm »
I'm sorry - that was a typo. The last bit should read:

procedure TForm1.btnDeleteClick(Sender: TObject);
.......

unit2.Form2.ShowModal;
  if canDelete then
    begin
      SQLQuery1.Delete;
      SQLQuery1.ApplyUpdates;
      SQLTransaction1.Commit;
    end;

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: Showmodal affects SQLQuery behavior
« Reply #4 on: August 16, 2023, 06:57:14 pm »
With QuestionDlg you can show the dialog in your own language. No need for separate form.

Code: Pascal  [Select][+][-]
  1. res := QuestionDlg('Title','Question?',
  2.               mtCustom,  // removes the bitmap
  3.              [mrYes,'YesOnYourLanguage',mrNo,'NoOnYourLanguage'],0);

Other examples see https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/questiondlg.html

Like
Code: Pascal  [Select][+][-]
  1. // custom modal result values and captions
  2. res := QuestionDlg('Question', 'Is it Okay?', mtConfirmation,
  3.  [400, 'Yes!!!', 401, 'Not cool', 402, 'My mistake'], 0);

But what stops withing with your code?

At least the Form2.Close is wrong if called via ShowModal.
Try setting ModalResult like I suggested.
https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tcustomform.showmodal.html

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #5 on: August 16, 2023, 09:32:24 pm »
You ask what does not work. When the user clicks on the "Yes" button the record should get deleted. This does not happen although it did work pefectly before with previous versions of Lazarus and earlier versions of the OS. Thanks for all your comments but I'm still mystified ...

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: Showmodal affects SQLQuery behavior
« Reply #6 on: August 16, 2023, 09:48:32 pm »
Then there is something else wrong.
It's not that code (although I still think the Form2.Close is wrong).

But we don't see the rest of the code.

You can put a showmessage('hello'); inside the begin/end to see if it gets there.
If it does, there is another reason why it's failing.
If it doesn't then your candelete isn't set correctly.

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #7 on: August 16, 2023, 11:02:13 pm »
I've done that and even made the showmessage "Hello" conditional upon canDelete = True so all the code executes correctly. I agree with you that something else is wrong but it's difficult to know what it is. Thanks for introducing me to TQuestionDlg - I didn't know about that - could be very useful indeed.
As regards Form2.Close, I've tried Self.Close and Close but the same happens. The record will not get deleted.
canDelete is set as a global variable in unit1.
Thanks for your comments - I appreciate them.
« Last Edit: August 16, 2023, 11:07:45 pm by mikeosullivan »

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: Showmodal affects SQLQuery behavior
« Reply #8 on: August 16, 2023, 11:22:47 pm »
As regards Form2.Close, I've tried Self.Close and Close but the same happens. The record will not get deleted.
No, it's not your actual problem but you should use
ModalResult := mrOk;
Or
ModalResult := mrCancel;
Instead of Form2.Close.

You can even use the result of ShowModal ( it's a function) instead of candelete.

This is better code.
Code: Pascal  [Select][+][-]
  1. if Form2.ShowModal = mrOk then
  2. begin
  3.   SQLQuery1.Delete;
  4.   SQLQuery1.ApplyUpdates;
  5.   SQLTransaction1.Commit;
  6. end;
and
Code: Pascal  [Select][+][-]
  1. procedure TForm2.btnYesClick(Sender: TObject);
  2. begin
  3.   ModalResult := mrOk;
  4. end;
  5.  
  6. procedure TForm2.btnNoClick(Sender: TObject);
  7. begin
  8.   ModalResult := mrCancel;
  9. end;
No need for candelete.

But as stated, your problem still remains.
There is probably something wrong with the deletesql statement.
(Or something else, but we can't see that without more code)

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #9 on: August 16, 2023, 11:37:25 pm »
I've tried your code but the same problem persists. I really don't understand what's going on here.

If I comment out the 2 lines

//unit2.Form2.ShowModal;
  //if canDelete then
.......

record deletion executes perfectly but without any warning to the user.
Thanks again for your help.

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: Showmodal affects SQLQuery behavior
« Reply #10 on: August 16, 2023, 11:38:15 pm »
And you said you tried to put a showmessage inside the begin/end?

Does this show the hello? Without deleting the record?

Code: Pascal  [Select][+][-]
  1. if Form2.ShowModal = mrOk then
  2. begin
  3.   SQLQuery1.Delete;
  4.   SQLQuery1.ApplyUpdates;
  5.   SQLTransaction1.Commit;
  6.   Showmessage('hello');
  7. end;

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #11 on: August 17, 2023, 08:35:00 am »
Yes, this code shows "hello" without deleting the record.
Here is a link to screenshots of the application before deletion, during deletion, showmessage, after deletion.
https://www.dropbox.com/scl/fo/1g4n6jpra8gl1d5iq4tru/h?rlkey=mhh6jcyt0wvuhlbgvtcnan3xr&dl=0
Thanks again for your help.

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: Showmodal affects SQLQuery behavior
« Reply #12 on: August 17, 2023, 09:02:05 am »
In sure what the delete should actually delete but if that background is your program then the calendar shouldn't change between the point of ShowModal and ShowMessage. And for you it does seem to change.

When ShowMessage shows your program is still in the delete code (begin/end) and couldn't have updated the calendar in the background. Unless you have a multi-threading application.

I think there might be some memory corruption going on in Form2.

Is Form2 currently initialized?
Did you not close it anywhere? (which could lead to corruption if you execute it again.)
Is this also happening the first time you run the other and click delete?
Or only on second time?

Show more code of TForm2 and the code where you create it.

You can also backup your code and go stripping until you find the problem.
When you stripped the code down and the problem persist you can post the complete code here.

Another option is to remove TForm2 and use the QuestionDlg.

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #13 on: August 17, 2023, 09:39:11 am »
rvk - you are right - the record does disappear during the process but reappears at the end. I don't have a multi-threaded application. You could well be right about memory corruption because I have older versions of this code (exactly the same) which I compiled some time ago which work perfectly. I only discovered this recently when I recompiled the code under Lazarus 2.2.4 runing on Monterey 12.6.8.
I agree with you that using QuestionDlg is a good workaround and I can live with that but I am still quite curious to understand what is going on here.
Thanks for all your help and expertise which I really appreciate but please don't spend any more time on this as I don't think we're going to get anywhere somehow. I will run the code on Mac (older OS), Linux and Windows virtual machines to probe further into the issue. If anything interesting turns up, I will post here again.
Thanks again for everything ...

mikeosullivan

  • New Member
  • *
  • Posts: 11
Re: Showmodal affects SQLQuery behavior
« Reply #14 on: August 17, 2023, 12:56:20 pm »
QuestionDlg does not work - it shows the dialog box and clicking on Yes closes the dialog but does not delete the record. The only dialog that works properly is MessageDlg. I don't get this at all ...

 

TinyPortal © 2005-2018