Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing the Master-Detail connection in Delphi

I have 2 tables which are connected using master-detail connection. I need the connection reversed on creation of Form2 - so the master table becomes the detail table, and the detail table becomes the master table.

I tried doing this, and the program compiles, but doesn't work the way I want it to (the previous connection breaks, but it's not reversed, so the program kinda works like the tables aren't connected at all):

    Form1.ADOTableDetail.MasterSource.Destroy;
    Form1.ADOTableMaster.MasterSource :=  Form1.DataSourceDetail;
    Form1.ADOTableMaster.MasterFields := 'the_field_that_connects_them';

Any ideas on how I might achieve this?

like image 714
Radiant Avatar asked Dec 08 '25 20:12

Radiant


1 Answers

Don't destroy the MasterSource!

In order to break the relationship do

Form1.ADOTableDetail.MasterSource:= nil;
Form1.ADOTableDetail.MasterFields:= '';

than use this to reroute the MasterDetail

Form1.ADOTableMaster.MasterSource :=  Form1.DataSourceDetail;
Form1.ADOTableMaster.MasterFields := 'the_field_that_connects_them';

Also never call .Destroy directly, use .Free instead.
Free does an extra check to see if the reference you are Freeing is not nil, preventing some Access Violations.

like image 184
Johan Avatar answered Dec 11 '25 12:12

Johan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!