Solved

access to objects on a form of a datamodule

Posted on 2006-11-06
4
173 Views
Last Modified: 2010-04-05
Hi, experts !

 - Delphi 2006
 - DataModule  (e.q. TDM_X1)
 - TADO... - components on the form  (e.g. qry1 = TADOQuery)
 - Public procedures defined in this unit  (e.g.  procedure TDM_X1.proc1)
 - Other procedures defined (only in implementation) in this unit  (e.g. procedure priv1)

If I use this component in a public procedure decared in this module everything is ok.

If I try to use such a component in a simple procedure (in this module !), qry1 is unknown. (example 1)     -->   _why ?  how to overcome this ?_

I tried to use local procedures inside a public procedure: it will work but this solution looks complicated.  (example 2)

//  1) -------------------------------------------------------------------------
  procedure TDM_X1.proc1;
  begin

      // code of TDM_X1.proc1

  end;


  procedure help1;
  begin
      ...
      qry1.SQL.Clear;           //  qry1 is unknown !
      ...
  end;


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



//  2) -------------------------------------------------------------------------
  procedure TDM_X1.proc1;

    procedure local1;
    begin
      ...
      qry1.SQL.Clear;           //  ok !
      ...
    end;

  begin

  // code of TDM_X1.proc1

  end;
----------------------------------------------------------------------------
0
Comment
Question by:KPBecker
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 17882017

It has to do with namespace scope. (Same as if dealing with a form).

- You can make help1 a procedure of the data module, eg TDM_X1.help1 and then access the qry1 directly
or
- Qualify the qry1 with the owning object, eg:

procedure help1;
 begin
      ...
      DM_X1.qry1.SQL.Clear;  
      ...
  end;

----

Regards,
Russell
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17882076

If the qry1 is on the form (and not the datamodule), then it would be

...
  FormX.qry1.SQL.Clear;
...

Just keep in mind the namespace scope (where the object declaration falls; eg global, within another class definition, etc).

Russell


0
 
LVL 7

Expert Comment

by:kfoster11
ID: 17882767
FormX is an object.  When you drop a control onto the form it becomes a property of the form or datamodule.  If you are accessing FormX from a procedure outside the scope of the form you will have to make sure that FormX has been created.

Proc Whatever;
begin
    if not Assigned(FormX) then
        exit;
end;

also this is not considered good programming but it will work.  It is better to add a procedure inside the form and call it from where you are using it because if for whatever reason you create multiple copies of an object your proc whatever will only access the globally declared instance.  And GLOBALS are BAD!

0
 

Author Comment

by:KPBecker
ID: 17888856
I thought that everything used in the unit of a datamodule or form belongs to the namespace of this form/datamodule.

I will give the points to Rusell, he was the first to answer and his solution works.

Thank you,
Russell and Kyle !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
creating manifest for my dll that called from activex 6 98
Delphi Mdi application Child forms get behind control 7 151
find a node in VST 2 63
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

14 Experts available now in Live!

Get 1:1 Help Now