Link to home
Start Free TrialLog in
Avatar of gspears060598
gspears060598

asked on

Dynamically creating Datamodule

Hi,

I have an application that needs three datamodules, all of the same type.  Each datamodule will have a different connection (username/password) to a common database.

These three datamodules need to be accessible from everywhere, i.e. global.  I have created a datamodule in the IDE, called Oracle_DM1.  I now want to create three "copies" of it.

The problem is that the first one gets created successfully, but the 2nd and the 3rd ones have a value of nil.

Here's the applicable code fragments...

The project source file...

-----------------------------------------------------------------------------------
program DBG;

uses
  Windows,
  SysUtils,
  Forms,
  Main in 'MAIN.PAS' {MainForm},
  Oracle_DataMod1 in 'Oracle_DataMod1.pas' {Oracle_DM1: TDataModule},
// others deleted here
  DataModStuff in 'DataModStuff.pas';

{$R *.RES}  

begin

  Application.CreateForm(TOracle_DM1, Oracle_DM1);  // seems to be OK
  Application.CreateForm(TOracle_DM1, Oracle_DM2);  // gets a value of nil
  Application.CreateForm(TOracle_DM1, Oracle_DM3);  // gets a value of nil
  Application.CreateForm(TMainForm, MainForm);          // Main Form
  Application.Run;
end.

------------------------------------------------------------------------------------------

unit Oracle_DataMod1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, OracleData, OraclewwData, Oracle;

type
  TOracle_DM1 = class(TDataModule)
    OracleSession1: TOracleSession;
    OracleLogon1: TOracleLogon;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Oracle_DM1: TOracle_DM1;
  Oracle_DM2: TOracle_DM1;  // I added this line
  Oracle_DM3: TOracle_DM1;  // I added this line

implementation

{$R *.DFM}

end.

----------------------------------------------------------------------------------------------------------------

Oracle_DM2 and Oracle_DM3 start off as nil but are set to a value when the MainForm.Create is ran. This form has in it's USES clause Oracle_DataMod1.  However, when I access the value of Oracle_DM2, even though the value does exist, or at least the IDE can read it, I get a Read Access Violation, and the app blows up.

I feel that the problem is either in the way I am creating the three data modules or a scoping issue.

Any idea what I am doing wrong? Environment is D3 with NT40

Thanks
Avatar of pjdb
pjdb

Can you try
Oracle_DM2:=TOracle_DM1.Create(Application);
instead of
Application.CreateForm(TOracle_DM1, Oracle_DM2)

However, you'll have to destroy the object yourself

JDB
ASKER CERTIFIED SOLUTION
Avatar of RJENKINS
RJENKINS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial