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

x
?
Solved

TQuery, EAccessViolation and Shared Forms

Posted on 1999-08-03
15
Medium Priority
?
207 Views
Last Modified: 2010-04-04
I'm trying to access a TQuery from a form that does not own it.  (I want to use a procedure that uses the query from different form).  The form where I make the request of the TQuery was created from the form that owns the TQuery. The form requesting the TQuery does reference the form owning the TQuery in the uses clause.  What am I doing wrong?   Thanks.
0
Comment
Question by:sartwj
[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
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 1392657
Does

  uses unit2;

  Form2.Query1.SQL := '<some query>';
  Form2.Query1.active := true;

not work?


0
 
LVL 2

Expert Comment

by:CalvinDay
ID: 1392658
The form with the TQuery has not been created or the variable name (form2) may not have the created form assigned to it.
0
 

Author Comment

by:sartwj
ID: 1392659
Epsylon:  No, produces an EAccessViolation.

CalvinDay: The form with the TQuery (form1) created form2 and still exists when form2 makes a request of TQuery on form1.   The code example of Epsylon's response is exactly what I use.

{form1}
procedure someTQueryapp;
  Query1.active := false;
  ....
procedure CreateForm2;
var
 form2 : Tform2;
begin
 form2 : Tform2.create(self);

{form2}
 uses unit2;
begin
 form1.someTQueryApp;
end;

I get the EAccessViolation when Delphi4 encouters Query1



0
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!

 

Author Comment

by:sartwj
ID: 1392660
I mistakenly put unit2 in the uses clause of my example, I meant unit1.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1392661
I'm not entirely sure what you are doing but you do not have to create Form2, just set the form2.visible to true.....
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392662
hi sartwj,
why not use a datamodule?
meikl
0
 

Author Comment

by:sartwj
ID: 1392663
I create the many forms dynamically in order to save memory.  This allow the user to create as many versions of the same form as they wish (to make multiple analyses).  I free the forms when the user is through with them (and they continue using other parts of the program).  Sharing procedures between dynamically created forms hasn't been any trouble except when using a TQuery.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1392664
Sartwj, all forms you create at design time are created automatically unless you state otherwise in Project menu > Options > Forms.
0
 

Author Comment

by:sartwj
ID: 1392665
kretzschmar:  

I hadn't heard of the datamodule.  I will give it a try!
Thanks.
0
 
LVL 7

Expert Comment

by:RBertora
ID: 1392666
where is the TForm1. in procedure SomTQueryApp?? did you just omit this in your typing? or are you using the interface part of unit1 as a declaration for a uses clause in unit2??

{form1}
procedure someTQueryapp;
  Query1.active := false;
  ....
{form2}
 uses unit2;
begin
 form1.someTQueryApp;
end;

I get the EAccessViolation when Delphi4 encouters Query1

This just means you are trying to access something that is not in memory or out of scope...

Why don't you post a full working example project?? write one up quickly and then post it in full(cut/paste) : unit1,unit2 that will make thing easier for the experts and you will get your answer faster...

Rob :-0)


0
 

Expert Comment

by:Tweety
ID: 1392667
Did you declare the variable form1 in the unit2, or is it realy the reference to form1?
Another Way you can try:
give a Reference of the Query to form2 using your own constructor for form2 or use a property.

I think it is better than a cross-reference.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1392668
If you want create the form dynamically, I think it must be something like this:

{form1}
procedure someTQueryapp;
  Query1.active := false;
  ....
procedure CreateForm2;
var
 form2 : Tform2;
begin
 form2 : Tform2.create(self);

{form2}
 uses unit2;
begin
 (parent as TForm1).someTQueryApp;
end;
0
 
LVL 2

Expert Comment

by:CalvinDay
ID: 1392669
procedure CreateForm2;
var
 form2 : Tform2; <<< This is what I was talking about (2nd part)
begin
 form2 : Tform2.create(self);

>>the variable name (form2) may not have the created form >>assigned to it.

when CreateForm2 is exited the temporary form2 variable goes away. Put it at the top of the unit.
0
 

Author Comment

by:sartwj
ID: 1392670
kretzschmar:  

DataModule was the answer.  Thank you.  Works great and organizes my non-visual data access objects in one container!

sartwj
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 400 total points
ID: 1392671
yup, for that are the datamodules, sartwj ;-)
glad that this helps you
good luck again

meikl
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

704 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