Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

access to objects on a form of a datamodule

Posted on 2006-11-06
4
Medium Priority
?
194 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
[X]
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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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…
Suggested Courses

671 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