Author Topic: Event handler procedure in different unit  (Read 196 times)


  • New Member
  • *
  • Posts: 25
Event handler procedure in different unit
« on: April 08, 2020, 01:36:46 pm »
In my current project, I use a data module which contains my dBase tables and associated DataSources.  I also have a Customers unit which contains a form and various related procedures.  I would like to have the OnDataChange event of the datasource in the datamodule run the procedure coded in the Customer module, but can't see how to do it without causing an error - is it even possible? I would prefer to have all the Customers related code all together in one unit without parts of it being in the datamodule unit - I suppose I could put the datasource in the Customers unit & keep the table in the datamodule, but it seems neater to have them together. Thoughts?


  • Hero Member
  • *****
  • Posts: 3078
Re: Event handler procedure in different unit
« Reply #1 on: April 08, 2020, 01:44:26 pm »
have a compatible event block of code, In the customers unit and direct set the event in the data module to it..

 TheDataModualUnit.DataModual.TheOnEvent := @CustomsUnit.Form.TheEventMethodThatisImplemented;

 Also you may want to test the sender of the event while processing the event so you'll know where It comes from...
The only true wisdom is knowing you know nothing


  • Sr. Member
  • ****
  • Posts: 407
Re: Event handler procedure in different unit
« Reply #2 on: April 08, 2020, 01:50:16 pm »
the datasource was introduced to help easily change the link between the dataaware controls and the dataset in a single assignment.
As such its proper place is in the module that uses it not the datamodule with the datasets. Each form /datamodule that needs access to a dataset through a datasource then that datasource needs to be on the datamodule using it not the datamodule that has the datasets.

Having said that you can connect any event handler to any event
regardless of where they are declared. If you use the designer to do so you need to pay close attention to the order of creation of the containers. for example if you have a datasource in datamodule1 which links to a dataset in datamodule2 it required that datamodule2 is created before the datamodule1.

You can see the order of creation in the "project options\forms" on the project options dialog.

Now things gets interesting if datamodule1 expects datamodule2 to be created and datamodule2 expects datamodule1 to be created as well. In that case move the datasource to the customers datamodule and avoid that kind of inter-dependencies   


TinyPortal © 2005-2018