Recent

Author Topic: [SOLVED] How to fix SQLQuery AfterScroll event with DataModule  (Read 365 times)

Landslyde

  • Full Member
  • ***
  • Posts: 183
  • Jesus lives!
I added a DataModule to my project, placed my connection, transaction and queries there, and my AfterScroll stopped working on the main form. I have a Customer DBGrid tied to another DBGrid, a master/detail configuration. As you traverse the Customer Grid, the other Grid displays information particular to that Customer. But now I get nothing when I scroll through the Customer Grid. I can only think of two possible fixes for this:

1. Add a DataSource and SQLQuery to the DBGrids needing AfterScroll, or;

2. Create the AfterScrolls in the DataModule unit.

But those are just guesses, that may or may not work. I'd like to know if there is another way to solve this. Any help will be appreciated. Thanks
« Last Edit: August 21, 2019, 01:16:19 am by Landslyde »
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

madref

  • Hero Member
  • *****
  • Posts: 681
  • ..... A day not Laughed is a day not Lived !!
    • Nursing With Humour
Re: How to fix SQLQuery AfterScroll event with DataModule
« Reply #1 on: August 20, 2019, 12:15:55 pm »
Try your guesses and see what happens.
I think option 1 is a good idea. Let it be depended of the CustumorID
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.2 / FPC 3.0.4
Lazarus Trunc / FPC 3.0.4
Mac OS X Mojave

Landslyde

  • Full Member
  • ***
  • Posts: 183
  • Jesus lives!
Re: How to fix SQLQuery AfterScroll event with DataModule
« Reply #2 on: August 20, 2019, 12:38:39 pm »
Thanks for the response.

So basically, even though I have DataSources and SQLQueries (now) on the DataModule for these two DBGrids on the Main Form, the only solution to make the AfterScroll event work is to add yet more DataSources and SQLQueries back to the Main Form?

I had all of this working fine, before seeing need for the DataModule. I'm using a PageControl that'll end up with abt six pages, each page with two or more DBGrids. So my main form was going to get super cluttered. That's why I moved all the db stuff to the DataModule. It's a shame that adding redundant DataSources and SQLQueries back to the main form is the only fix to this. But I'll take your word on it and get 'er done. Thanks again.
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 795
Re: How to fix SQLQuery AfterScroll event with DataModule
« Reply #3 on: August 20, 2019, 03:21:56 pm »
I added a DataModule to my project, placed my connection, transaction and queries there, and my AfterScroll stopped working on the main form.
All DB related events must be also created and content transferred to the DataModule.

lucamar

  • Hero Member
  • *****
  • Posts: 1986
Re: How to fix SQLQuery AfterScroll event with DataModule
« Reply #4 on: August 20, 2019, 03:31:16 pm »
The only "problem" I see is that by moving the data-access controls and its events to the data-module you no longer have access by default to the form's controls, but that can be easily remedied by adding the form's unit to the uses clause in the implementation section of the data module and qualifying the accesses you make to the form's controls.

You might, perhaps, make it easier by adding some auxiliary methods to the form like, say: function GetClientsGrid: TDBGrid or whatever, to be able to work through the class rather than the instance, but that's usuallly just gilding that can be added as needed.

Depending on your application you'll have a little or a lot of work to do and some re-thinking time to dedicate, but that's common to almost any refactoring.

My recomendation, in short, is to move the event handlers to the same unit where its class resides: which in this case, for AfterScroll is the data module
« Last Edit: August 20, 2019, 03:34:29 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Landslyde

  • Full Member
  • ***
  • Posts: 183
  • Jesus lives!
Re: How to fix SQLQuery AfterScroll event with DataModule
« Reply #5 on: August 21, 2019, 01:15:56 am »
My recomendation, in short, is to move the event handlers to the same unit where its class resides: which in this case, for AfterScroll is the data module

That's what I ended up having to do. As a matter of fact, I completely did away with the DataModule and put the connection, transaction, datasource and queries back on the main form...all stacked one on top of the other. Anything I do with them is done through code or the Object Inspector anyway. So, problem solved.

Thanks for the input, Forum.

PS: slightly used DataModule for sale on the cheap  :D
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

Landslyde

  • Full Member
  • ***
  • Posts: 183
  • Jesus lives!
Re: [SOLVED] How to fix SQLQuery AfterScroll event with DataModule
« Reply #6 on: August 21, 2019, 02:53:41 am »
Although I haven't tried it, I jut found a possible one-line solution on SO:

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   DataModule1.SQLQuery1.AfterScroll := MyAfterScrollHandler;
  4. end;
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.