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
Solved

Dynamically creating Datamodule

Posted on 1998-10-04
2
534 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process Javascript errors with Delphi TWebBrowser 1 173
When I am typing into a TEdit last word repeats 7 145
Delphi...Split view - idea? 1 87
Wincontrol not (correctly) drawn 15 40
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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