Board index » delphi » Datamodule in MDI-forms

Datamodule in MDI-forms

We are writing a MDI-application. In the private part of our MDI-Child
form we have a (pointer to a ) datamodule.

In the formcreate we create the module (somedatamodule.Create(self) )

In the formdestroy we free the module (somedatamodule.free)

So every MDI-child has its own datamodule (we thought). But when we
navigate through one MDI-Child-window the other MDI-child window's
datasource changes allso.
Are we doing something wrong, is it a bug or ???

Ramon Leeflang
Softstar
The Netherlands

Please email to : softs...@telebyte.nl

 

Re:Datamodule in MDI-forms


Quote
softs...@telebyte.nl (softstar) wrote:
>We are writing a MDI-application. In the private part of our MDI-Child
>form we have a (pointer to a ) datamodule.

>In the formcreate we create the module (somedatamodule.Create(self) )

>In the formdestroy we free the module (somedatamodule.free)

>So every MDI-child has its own datamodule (we thought). But when we
>navigate through one MDI-Child-window the other MDI-child window's
>datasource changes allso.
>Are we doing something wrong, is it a bug or ???

No, this isn't a bug. The problem, is that you are referencing the global
variable for your datamodule that Delphi creates for it automatically.
Thus, you are only ever talking to the same datamodule, even though you are
creating many of them. What you want to be doing, is referencing the
instance variable, which in this case is most logically stored in your
form. The following example assumes you have the datasource components on
the form itself -- this is the easiest method:

[caveat: I have tried the technique below and it works, but the code itself
is typed from memory, so there may be errors.]

TMyForm = class(TForm)
  //IDE managed definitions
  private
    FDataModule: TMyDatamodule;
  public
    procedure ConnectDatasources;
etc.

procedure TMyForm.FormCreate
  FDataModule := TMyDatamodule.Create;
  ConnectDatasources;

procedure TMyForm.FormDestroy
  FDataModule.Free;

procedure TMyForm.ConnectDatasources;
begin
  { this assumes you are using 3 datasource components on your form }
  Datasource1.Dataset := FDataModule.Tabel1;
  Datasource2.Dataset := FDataModule.Query2;
  Datasource3.Dataset := FDataModule.Query3;
end;

Note that you should still keep the global variable on the datamodule, and
you can still assign this at design time. This lets you see the data live
at design time. The connections will get replaced at run time, by the code
above.

Hope this helps.

--
Brad Aisa <ba...@hookup.net>
Business Machine Interfaces, Inc.
Toronto, ON, Canada

Other Threads