Solved

access to objects on a form of a datamodule

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

18 Experts available now in Live!

Get 1:1 Help Now