I am using Delphi6
and have a data module with an ADO
DataSet
which is used by two forms, formA and FormB. Each form has a Dataset.Open()
in OnCreate
and Dataset.Close()
in OnClose
. If both forms are open simultaneously and formB is closed the dataset is closed in formA. How can I prevent this, essentially I need separate instances of the dataset for each form but at the same time use the datamodule.
The simplest way to achieve what you want is to create an instance of the data module for each form, and pass it to the form so it can be freed when the form is closed:
var
Data: TDataModule;
begin
Data := T<YourDataModule>.Create(Self);
try
Form := T<YourForm>.Create(Self);
Form.DataModule := Data;
Data.Name := '';
except
Data.Free;
raise;
end;
Form.Show;
end;
Setting the DataModule's Name to an empty string is done to ensure that the VCL's logic for hooking up data aware controls to their datasource/dataset is done using the newly created instance, instead of the first ever instance.
In the Form's OnClose handler (or its destructor) make sure to free the data module.