Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using a datamodule and DBGrid

612 views
Skip to first unread message

Kjell Hansen

unread,
Jun 29, 1999, 3:00:00 AM6/29/99
to
Hi,
I'm quite new to Delphi and haven't read all headers through so I might
ask a very often asked question, pls excuse me for this.

I like to keep all my SQL in a few datamodules and don't spread them
around in all forms. I have made a datamodule containing a
TQuery-component and a TDatasource-component. The TQuery have a default
SQL-statement which I want to use in a DBGrid and at design time I see
the records but when I run the program it's all blank.
What obvious things am I missing?

TIA
Kjell Hansen


John Elrick

unread,
Jun 29, 1999, 3:00:00 AM6/29/99
to
I'm assuming you mean you've set the Active property to true at Design time
and then run the application. In D3, the Active state is stored from Design
time to runtime so your data grid should still show the information at run
time, a few quick things to check.

Are you auto creating the data module, or creating at run time? If the DM
doesn't exist you'll get an empty grid.
Could you be closing the Query during start up or any other time and then
not reopening?

You could try adding a Query.Open to your DataModule Create handler. See
what that does.


--
John Elrick
jo...@improgrammer.com
www.improgrammer.com

Delphi Application/Component Development
Delphi Mentoring

Kjell Hansen <kjell....@telia.com> wrote in message
news:377918A8...@telia.com...

Bill Todd

unread,
Jun 29, 1999, 3:00:00 AM6/29/99
to
Is the query's Active property set to True at design time when you compile
your program? If not you will need code to explicitly open the query. The
data module's OnCreate event handler is a good place to do this. Also, make
sure the data module is being created. It should be in the Auto Create forms
list in the Project | Options dialog or you must have code that creates the
data module.

Do you get any error messages when you run the program? Is it possible that
the DataSource component's DataSet property is being set to nil in your
code?

Bill

--

Bill Todd - TeamB
(TeamB cannot respond to email questions. To contact me
for any other reason remove nospam from my address.)

Kjell Hansen

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
Hi
Thanx for trying to help, but it still doesn't work out.
1. The Active property is True at design time (I can see the grid values)
2. The module's not auto created (I think it's a waste creating a dozen modules
and maybe not use them)
3. On opening the ShowList I get TDataSource class not found

This is the code of the ShowList form....

unit frmShowListU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, ComCtrls, ToolWin, Db, DBTables, Grids, DBGrids;

type
TfrmShowList = class(TForm)
grdMedlemslista: TDBGrid;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);

implementation

uses modMembersListU;

var
oMembersList : TmodMembersListU;
ds :TDataSource;


procedure TfrmShowList.FormCreate(Sender: TObject);
begin

oMembersList := TmodMembersListU.Create(self);
oMembersList.qMembersList.Active := True;

ds := TDataSource.Create(Self);
ds.DataSet := oMembersList.qMembersList;
grdMembersList.DataSource := ds;
end;

procedure TfrmVisaLista.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ds.Free;
oMembersList.Free;
Action := caFree;
end;

end.

Any suggestions, anyone?

Regards
/Kjell


John Elrick

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
Question. If you already have DataSource created at design time, why are
you dynamically creating one? Since you can't use the form without it, and
you are destroying the form when you're finished, it looks like you're
making more work for yourself - and possible introducing this problem.

Try using your designtime datasource instead and see if it works properly.
Although, I have to admit I haven't caught any obvious errors in your code.

John Elrick


Kjell Hansen <kjell....@telia.com> wrote in message

news:377A6227...@telia.com...

Kjell Hansen

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
Sorry John,
That's my first approach and since it didn't work I added the code.
Another reason is that I in the next step will have a dropdown list where you
chose which year that will be the one yu're showing members from.
And I guess you can't set the filter if you don't have created the datamodule.
IMHO.

/Kjell

Bill Todd

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
Att design time double click the grid to open the Columns Editor and add
columns for all of the fields you want to appear in the grid. Also check
the Fields Editor and make sure that no field objects have been created or
that field objects have been created for all of the fields you want to
appear in the grid.

Kjell Hansen

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
Hi again,
thanx again for your efforts :)
But I see the grid the way I want it at design time. If I hadn't I would be
less frustrated (well... maybe not) and I would have had more understanding
that it wouldn't show up at runtime.

TIA
/Kjell

Jan Sprengers

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to
On Wed, 30 Jun 1999 20:29:59 +0200, Kjell Hansen
<kjell....@telia.com> wrote:

>1. The Active property is True at design time (I can see the grid values)
>2. The module's not auto created (I think it's a waste creating a dozen modules
>and maybe not use them)

If you see the data at design time while Active is true, it must be
that the datamodule is referenced like 'modMembersListU' (the
datamodule's class name without 'T'). This is global variable that
Delphi automatically adds for referencing autocreated
forms/datamodules.
So a possible solution is to create the datamodule before the form and
assign the resulting object to the global variable 'modMembersListU'.

>3. On opening the ShowList I get TDataSource class not found

Where exactly? What is the exact error message?

Jan


John Elrick

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to
It would seem to me, after reading the entire thread, that we're missing
something here. For example, I don't quite understand how having a dropdown
list has anything whatsoever to do with the creation order of an object.

Perhaps it would help if you could explain specifically the intention of
this section of your application.

John

Kjell Hansen <kjell....@telia.com> wrote in message

news:377A6792...@telia.com...

> > > 1. The Active property is True at design time (I can see the grid
values)
> > > 2. The module's not auto created (I think it's a waste creating a
dozen
> > modules
> > > and maybe not use them)

> > > 3. On opening the ShowList I get TDataSource class not found
> > >

Kjell Hansen

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
Hi all, (and John Elrick specifically)
This section of my program will show who was a member in the union a specific
year. I have a Persons-table and a MemberYear-table which I'm joining on
membershipnumber and showing the result in a grid.
That's what the dropdown are doing in the form

Regards
/kjell

Kjell Hansen

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
Hi Jan!
Your ideas makes sense to me, but...Being the beginner I am... How?
It's a MDI-program where I'm opening the DBgrid-form as a child.

The Error message appears just before the MDI-child is shown and after I've had to
type the password for the database alias. And it says "Class TDataSource not found"
with the Red-and-white-Critical-error-cross and an "OK" button.
Somehow the program won't continue to function so I have to Ctrl+F2 to close it.

I'm really appreciating the time you're taking to help. Hopefully I will contribute
when I get more knowledge.

Regards
Kjell

Jan Sprengers wrote:

> On Wed, 30 Jun 1999 20:29:59 +0200, Kjell Hansen
> <kjell....@telia.com> wrote:
>

> >1. The Active property is True at design time (I can see the grid values)
> >2. The module's not auto created (I think it's a waste creating a dozen modules
> >and maybe not use them)
>

> If you see the data at design time while Active is true, it must be
> that the datamodule is referenced like 'modMembersListU' (the
> datamodule's class name without 'T'). This is global variable that
> Delphi automatically adds for referencing autocreated
> forms/datamodules.
> So a possible solution is to create the datamodule before the form and
> assign the resulting object to the global variable 'modMembersListU'.
>

> >3. On opening the ShowList I get TDataSource class not found
>

John Elrick

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
I can understand what you are attempting to display - but I still am
confused as to why you are creating the DataSource at runtime? Or how the
creation of the dropdown affects the creation of the DataSource? Your
answer doesn't address this question.

It seems to me you could use this:

Kjell Hansen <kjell....@telia.com> wrote in message
news:3780E1F5...@telia.com...

John Elrick

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
Darn Outlook. Forget the prior post!

I can understand what you are attempting to display - but I still am
confused as to why you are creating the DataSource at runtime? Or how the
creation of the dropdown affects the creation of the DataSource? Your

answer doesn't address these questions.

It seems to me you could use this:


unit frmShowListU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms,
Dialogs,
StdCtrls, ComCtrls, ToolWin, Db, DBTables, Grids, DBGrids;

type
TfrmShowList = class(TForm)
grdMedlemslista: TDBGrid;

ds : TDataSource; << Declared at design time


procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action:
TCloseAction);

implementation

uses modMembersListU;

var
oMembersList : TmodMembersListU;


procedure TfrmShowList.FormCreate(Sender: TObject);
begin

oMembersList := TmodMembersListU.Create(self);
oMembersList.qMembersList.Active := True;


ds.DataSet := oMembersList.qMembersList;
end;

procedure TfrmVisaLista.FormClose(Sender: TObject;
var Action: TCloseAction);
begin

oMembersList.Free;
Action := caFree;
end;

end.


and eliminate the dynamic creation. I only say this because the approach
you've taken implies you may be doing other things which could be affecting
your form. Plus, you've stated you can "see" the data at design time which
means your grid is already attached to a datasource, yet your form
declaration doesn't show one and you seem to not be able to use it at
runtime. So I ask again, why specifically are you creating a datasource at
runtime?

John

Jan Sprengers

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
On Mon, 05 Jul 1999 19:51:35 +0200, Kjell Hansen
<kjell....@telia.com> wrote:

>Your ideas makes sense to me, but...Being the beginner I am... How?
>It's a MDI-program where I'm opening the DBgrid-form as a child.

If it is an MDI application, it probably doesn't make much sense what
I said about assigning the created datamodule to the global variable
(except as a test case).

Since MDI programs typically allow a window to be open more than once,
you can't rely on global variables for referencing. The approach you
took by dynamically creating the datamodule is good, but you should
probably assign it to a variable of the form's class, not another
(static) global variable.

>The Error message appears just before the MDI-child is shown and after I've had to
>type the password for the database alias. And it says "Class TDataSource not found"
>with the Red-and-white-Critical-error-cross and an "OK" button.

Where is your TDataSource located at design time? On the form or the
datamodule? Check if it has a corresponding variable in the published
part of the form/datamodule. If it is missing, delete the component
and recreate it.

HTH,

Jan


Kjell Hansen

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
Hi,
Thank you again for trying John,
Why I'm creating the datasource?
Don't know, actually. I thought I had to.
My main goal is to keep all SQL and datacomponents in the datamodule. But
adding a datasource to the form (and removing it from the datamodule) did the
trick.

The dropdowncombo isn't implemented yet, but I hope I can use the same
Datasource. I'm planning on calling a function in the datamodule which returns
a TStringList to fill the combo and changing the year in the combo will trigger
an event where I re-read the grid with the selected year instead of the
current.

I'm still puzzled, though, if this is the right approach. The question is if
it's applyable for larger multiusers database applications? This is the first
thing I'm doing in Delphi and it's a single user application but the next step
will probably be for multiple users and I like to do things the scalable way :)

Any advices are greatly appreciated.

Again, many thanx, especially to you, John.

Regards
Kjell Hansen

John Elrick

unread,
Jul 5, 1999, 3:00:00 AM7/5/99
to
Yes, using public functions and procedures in a data module is definately
the correct approach. Makes a little more work up front - if you can call
declaring a procedure "work", but it makes it a lot more flexible in the
long run.

Good luck and let me know if you have any more questions.

John


Kjell Hansen <kjell....@telia.com> wrote in message

news:37810D8E...@telia.com...

0 new messages