Solved

Referencing objects on an ActiveForm from another unit

Posted on 2001-08-27
6
379 Views
Last Modified: 2010-04-06
Easy to do with a normal TForm and unit - ie in the second unit have a "uses uMain" where uMain defines for example "var MyMainForm:TMyMainForm" and within the second unit we can have MyMainForm.memo1.lines.text:='xx'

I'm trying to do this with an ActiveX Form:
I have a TMyAForm=class(TActiveForm, IMyAForm) and a datamodule unit - everythin has been working a treat so far, opening database connection etc - but now I require one of the query components in the datamodule to alter a property of an object on the main form.

Any help would be greatly appreciated.
0
Comment
Question by:djmcrae
6 Comments
 

Author Comment

by:djmcrae
ID: 6428760
The ActiveXForm is being compiled as an OCX! (forgot to mention)
0
 

Expert Comment

by:alshaikh
ID: 6430787
From my own experiments, I don't think it is possible to use more than one form in an ActiveFrom project. In our own projects, we try to put all components and functionality we need in just one ActiveForm and use the Visible property to show/hide these components at run time.

I noticed however, that it is possible to have more than one form, provided that the second form is created at run time dynamically. We have not explored this in details yet.

Regards,
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6431720
I don't think it's possible. Besides, I think it's bad programming to link units that way.

Pass the Query object (or the specific property) to the function in the 2nd unit, so that function can change the property that belongs to the query object on the main form.

example:

unit Unit2;

...

procedure ChangeQuery(sql: TStrings);
begin
  sql.Text := 'select * from table1';
end;


Call this function from the main unit like this:

uses Unit2;

ChangeQuery(Query1.SQL);
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:djmcrae
ID: 6432828
 It may not be possible - I will try for just a bit longer though (~1 week). I have a huge application - the main form is 7,500 lines of code (it also has 8 other units, although these are much smaller). I want to shift the dozen odd database components to a datamodule (this worked OK) and move a fair bit of database code from the main to this. This has been proceeding along well, until one of the TADOQuery's onOpen code was moved over.
  This onOpen refreshed the strings in a TComboBox on the main form - grief - cannot get to the objects on the main form. I could just have the onOpen populate a TStringList which the opening code in main could the reference after issueing the opening command - yuk!!
  Another work around may be to make the whole main ActiveForm a normal TForm, then have the activeForm create an instance of this (outlined at http://pw2.netcom.com/~cherrman/cvtforms.htm) - better choice than my first, and much better than...
  Have a global var in the main pointing to the object on the form, ie var objPersonnel:TDBComboBox; then in the ActiveForm's onCreate, assign objPersonnel:=cbPersonnel ir the combobox on the form, the dataModule then uses objPersonnel. I just tried this and, dammit, it works. I do not like it one bit!! Global variables for reference by other units, it cannot be good!!
  Hitting the books at the moment on the COM world - we may be able to use an interface - my knowledge here could do with some honing!!
  BTW, alshaikh, creating a second form is not a problem - this application has about 8 including an about form, & not including the many messageboxes - it's handled no differently from normal.
0
 

Author Comment

by:djmcrae
ID: 6432971
 It may not be possible - I will try for just a bit longer though (~1 week). I have a huge application - the main form is 7,500 lines of code (it also has 8 other units, although these are much smaller). I want to shift the dozen odd database components to a datamodule (this worked OK) and move a fair bit of database code from the main to this. This has been proceeding along well, until one of the TADOQuery's onOpen code was moved over.
  This onOpen refreshed the strings in a TComboBox on the main form - grief - cannot get to the objects on the main form. I could just have the onOpen populate a TStringList which the opening code in main could the reference after issueing the opening command - yuk!!
  Another work around may be to make the whole main ActiveForm a normal TForm, then have the activeForm create an instance of this (outlined at http://pw2.netcom.com/~cherrman/cvtforms.htm) - better choice than my first, and much better than...
  Have a global var in the main pointing to the object on the form, ie var objPersonnel:TDBComboBox; then in the ActiveForm's onCreate, assign objPersonnel:=cbPersonnel ir the combobox on the form, the dataModule then uses objPersonnel. I just tried this and, dammit, it works. I do not like it one bit!! Global variables for reference by other units, it cannot be good!!
  Hitting the books at the moment on the COM world - we may be able to use an interface - my knowledge here could do with some honing!!
  BTW, alshaikh, creating a second form is not a problem - this application has about 8 including an about form, & not including the many messageboxes - it's handled no differently from normal.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9288587
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept Epsylon's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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 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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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