error message “Connection is not defined”

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

error message “Connection is not defined”

Post by scanc » Sat 14 Mar 2009 09:58

Data Module and error message “Connection is not defined” in Form Create

I have a DataModule with the TMSConnection in autocreate.
In the Data Module i have a TMSQuery1 with a Data Source1.
I have a Form1 that opens then TMSQuery1 in Data Module and call a second form.
In the second form i have a TDBEdit connected at DataSource1 in Data Module with an onChangeEvent that opens another TMSQuery2 in Form2.
When I create a Form2, I receive a message “Connection is not defined” opening TMSQyery2.
The same application works fine in BDE

I tried to solve this problem in 2 way :
1) Before opening Form2 i close TMSQuery1.
2) I move the DataSource1 from DataModule to Form2

I think these solutions are’nt acceptable
Does somebody can tell me why happens with SDAC component and not with BDE ?
Does somebody knows another better solution on this problem ?

Thanks in advance

Matteo

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Tue 17 Mar 2009 13:30

Do you assign a value to the Connection property of MSQuery2 in Form2?

scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

Post by scanc » Tue 17 Mar 2009 17:12

yes i did it..

baturkilic
Posts: 2
Joined: Wed 18 Mar 2009 09:49

Post by baturkilic » Wed 18 Mar 2009 09:57

did you check form create order.

scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

Post by scanc » Sat 21 Mar 2009 09:10

the creatione order of the forms is correct. Now i show tou the Code..

scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

Post by scanc » Sat 21 Mar 2009 09:10

the creation order of the forms is correct.
Now i show you the code.

scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

Post by scanc » Sat 21 Mar 2009 09:11

the creation order of the forms is correct.
Now i show you the code.

scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

Post by scanc » Sat 21 Mar 2009 09:19

this is the Datamodule that contains the databases and is the first autocreate form:

unit Y5DmConn;

interface

uses
SysUtils, Classes, DB, DBAccess, MSAccess;

type
TY5UDmConn = class(TDataModule)
Database: TMSConnection;
dbQTemp: TMSConnection;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Y5UDmConn: TY5UDmConn;

implementation

uses Y5DynControlUser;

{$R *.dfm}

procedure TY5UDmConn.DataModuleCreate(Sender: TObject);
begin
RTVUsrPswIdysnet(Database);
RTVUsrPswQtemp(dbQTemp);
end;

procedure TY5UDmConn.DataModuleDestroy(Sender: TObject);
begin
Database.Close;
dbQTemp.Close;
end;

------------------------------------------
This is a second DataModule on the auto create that contains TMSQuery and TDataSource and opens the TDatabase:

unit DM;

interface

uses
SysUtils, Classes, DB, MemDS, DBAccess, MSAccess;

type
TUDM = class(TDataModule)
IVA: TMSQuery;
DataSource1: TDataSource;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
UDM: TUDM;

implementation

uses Y5DmConn;

{$R *.dfm}

procedure TUDM.DataModuleCreate(Sender: TObject);
begin
Y5UDmConn.Database.Open;
end;

end.

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

This is the MainForm on the autocreate:

unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TFmMain = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FmMain: TFmMain;

implementation

uses Unit1, DM, Y5DmConn;

{$R *.dfm}

procedure TFmMain.Button1Click(Sender: TObject);
begin
Form1:=TForm1.create(application);
Form1.ShowModal;
end;

procedure TFmMain.FormCreate(Sender: TObject);
begin
UDM.IVA.Open;
end;

end.

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

this is the second Form in the Available Forms:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, MemDS, DBAccess, MSAccess, StdCtrls, Mask, DBCtrls;

type
TForm1 = class(TForm)
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
cli: TMSQuery;
procedure DBEdit1Change(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses DM, Y5DmConn;

{$R *.dfm}

procedure TForm1.DBEdit1Change(Sender: TObject);
begin
CLI.CLOSE;
CLI.OPEN; //Here the error is show!!!'Connection is not Defined'
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ACTION:=CAfREE;
Form1:=nil;
end;

end.

----------------------------
I noticed that if i put the TDatasource in form1 the error does not appear but i can't do this..the TDatasource must be on the DataModule (DM) but it raise the error..

scanc
Posts: 9
Joined: Thu 05 Feb 2009 13:23

Post by scanc » Sat 21 Mar 2009 09:23

in the Y5DmConn when i call RtvUserPsw i do this

db.Connected:=false;
db.Username:=User;
db.Password:=Password;
db.LoginPrompt:=false;
db.Server:=NomeServer;
db.Options.ApplicationName:=ExtractFileName(Application.ExeName)
db.Options.NumericType:=ntBCD;
db.Options.DefaultLockTimeout:=180000;

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 23 Mar 2009 11:47

Please send me a complete sample to dmitryg*devart*com to demonstrate the problem.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: error message “Connection is not defined”

Post by marsheng » Fri 29 Jun 2012 10:24

I had the same problem. However mine came from moving the MyConnection from Login form to the DM and not updating the Connection in the tables.

All working now.

Post Reply