Solved

Dynamically creating Datamodule

Posted on 1998-10-04
2
521 Views
Last Modified: 2010-04-03
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
0
Comment
Question by:gspears060598
2 Comments
 
LVL 1

Expert Comment

by:pjdb
ID: 1341769
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
0
 
LVL 1

Accepted Solution

by:
RJENKINS earned 50 total points
ID: 1341770
Inherit the second and third DataModules from the original DM
Goto File| New..
and select the tabwith you projects name
thisa will create 3 different Unit files which will have to be saved seperately

Remember only to make changes to the first (original DataModule)
this will keep them in sync

Hope this Helps
RCJ
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now