Recent

Author Topic: [SOLVED] Question about Data Module  (Read 2342 times)

440bx

  • Hero Member
  • *****
  • Posts: 4002
Re: Question about Data Module
« Reply #15 on: February 21, 2024, 04:42:53 am »
They may matter when dependencies exist among the units in the uses clause.
They also matter when multiple units interface functions or procedures with the same name.  Depending on the order, the wrong function or procedure may end up being called.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

egsuh

  • Hero Member
  • *****
  • Posts: 1289
Re: Question about Data Module
« Reply #16 on: February 21, 2024, 04:53:01 am »
Quote
They also matter when multiple units interface functions or procedures with the same name.  Depending on the order, the wrong function or procedure may end up being called.

Yes, it's true. I think we should try to avoid those designs.

1HuntnMan

  • Full Member
  • ***
  • Posts: 181
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Question about Data Module
« Reply #17 on: February 22, 2024, 08:00:16 pm »
Okay, earlier this morning, I have a Data Module that works.  Thanks all for all the great comments and advice.  The big thing that I learned is the Data Module HAS TO be created in your app.lpr BEFORE you create your main form.  Also, putting the Data Module as your first USE unit in your data forms.  Also, I discovered that ShowModal was a bit of an issue for Data forms.  Non-Data forms, i.e. an About (your app.) form I use ShowModal. I just changed the data forms to just form.show. ...Take Care

TRon

  • Hero Member
  • *****
  • Posts: 2483
Re: Question about Data Module
« Reply #18 on: February 22, 2024, 08:12:53 pm »
The big thing that I learned is the Data Module HAS TO be created in your app.lpr BEFORE you create your main form.
In that case, sorry to say, you learned wrong.

Quote
Also, putting the Data Module as your first USE unit in your data forms.
Same answer as above.

Quote
Also, I discovered that ShowModal was a bit of an issue for Data forms.  Non-Data forms, i.e. an About (your app.) form I use ShowModal. I just changed the data forms to just form.show. ...Take Care
That indicates that the flow your program depends on how you have set your (database) properties at design time.

All the conclusions you have made so far depend solely on that, and nothing else. /that/ is the thing you should have learned  :)

And I am sorry to have to say it the way I just did above but I failed to see how that message could be softened other than to say: yes you are right in your specific situation because you choose to use that (program) flow.
« Last Edit: February 22, 2024, 08:28:49 pm by TRon »

egsuh

  • Hero Member
  • *****
  • Posts: 1289
Re: [SOLVED] Question about Data Module
« Reply #19 on: February 23, 2024, 06:32:53 am »
You'll learn, but to explain a bit more:

Quote
The big thing that I learned is the Data Module HAS TO be created in your app.lpr BEFORE you create your main form. 

If your forms do not access anything in data module at the creation of forms, data module doesn't have to be created before forms. In your case, I think datasets (TSQLQuery, TBufDataSet, etc.)  are on data module, TDataSource and data controls  (TDBGrid, TDBEdit, etc.) are on forms, and your datasource's  dataset is pointing to datasets on the data module (hard coded). In this case, at the creation  of forms, it tries to access data module, and data module should have been created before the creation of forms. But if you assign datasource's dataset property at runtime, you may created data module within the forms.  For example,

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.     DM1 := TDM1.Create (nil);
  4.     DataSource1.DataSet := DM1.SQLQuery1;
  5.     DM1.SQLQuery1.Open;
  6. end;


Quote
Also, putting the Data Module as your first USE unit in your data forms. 

Generally this is not important.

Quote
ShowModal was a bit of an issue for Data forms.  Non-Data forms, i.e. an About (your app.) form I use ShowModal. I just changed the data forms to just form.show.


With ShowModal, the modal form is not destroyed at close until you free it explicitly. I made following example:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.    with Tform2.create (nil) do begin
  4.       ShowModal;
  5.       if ModalResult = mrOK   // this form is closed at this point
  6.            then showmessage(Edit1.Caption); // Edit1 is on TForm2, because of "with"
  7.       Free;   // and form is freed here.
  8.       //  if edit1.caption = ..    // this will cause invalid access, because the form has been freed.
  9.    end;
  10. end;
  11.  

I attached the sample project. Here, you have to notice:

1) In project options, form1 is auto-created while form2 is just available.
2) On TForm2, btn1.ModalResult = mrOK, and btn2.ModalResult = mrCancel (at object inspector).

Joanna

  • Hero Member
  • *****
  • Posts: 743
Re: [SOLVED] Question about Data Module
« Reply #20 on: February 24, 2024, 12:22:33 am »
It’s good that he got it to work regardless. He can always improve later.
When I was having problems with trying to use datamodule code before it was created I also tried creating the data module before the other forms but it didn’t help. Apparently the units are initialized before any form is created including the data module?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018