Solved

access to objects on a form of a datamodule

Posted on 2006-11-06
4
181 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 70
oracle global variables 4 80
Why can't I create any project with my delphi EXE file? 6 58
DBCtrlGrid, Delphi, Scroll 7 35
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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