Solved

Dynamically creating Datamodule

Posted on 1998-10-04
2
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 106
Firemonkey android show image from resource ? 1 56
Multi-layered image in FireMonkey 9 68
Twebbrowser in Fire monkey android scrolling to bottom 1 68
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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