Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

procedure TForm1.ExchangeMasterDetail;
begin
  ADOTableDetail.Close;
  ADOTableMaster.Close;
  ADOTableMaster.MasterFields := ADOTableDetail.IndexFieldNames;
  ADOTableMaster.IndexFieldNames := ADOTableDetail.MasterFields;
  ADOTableDetail.IndexFieldNames := '';
  ADOTableDetail.MasterFields := '';
  ADOTableDetail.MasterSource := nil;
  ADOTableMaster.MasterSource := DataSourceDetail;
  ADOTableDetail.Open;
  ADOTableMaster.Open;
end;

Just set the Master table Active propriety to false.

Then do what you wanna todo and set it back to true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top