Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

access to objects on a form of a datamodule

Posted on 2006-11-06
4
Medium Priority
?
204 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 750 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

972 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