[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Evaluate a String Expression

Posted on 2004-04-23
7
Medium Priority
?
440 Views
Last Modified: 2010-04-05
Greetings,

I want to have a function that will check if 7 diferent tables are empty without having to specify the commands 7 times. Would be something like this:

procedure TForm1.OpenDBExecute(Sender: TObject);
var
  ct : integer ;
  dummys: string ;
begin
  for ct := 1 to 7 do
  begin
    dummys := 'Form1.QAgenda' + inttostr(dummys) + '.IsEmpty';
    if MyFunction( dummys) then
      showmessage( 'table is empty' ) ;
  end;
end;

what I want is not to have to write
if Form1.QAgenda1.IsEmpty then
  ...
if Form2.QAgenda1.IsEmpty then
  ...
if Form3.QAgenda1.IsEmpty then
  ...
if Form4.QAgenda1.IsEmpty then
  ...
if Form5.QAgenda1.IsEmpty then
  ...
if Form6.QAgenda1.IsEmpty then
  ...
if Form7.QAgenda1.IsEmpty then
  ...

This is a very basic question but I couldnt find the way how to evaluate the expression

Thanks for any help
0
Comment
Question by:ROGERPORT
  • 5
  • 2
7 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10902702
Something like this maybe

  for I:= 1 to 7 do
  if TTable(FindComponent(TTable + inttostr(I))).isEmpty
   then


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10902716
oops

for I:= 1 to 7 do
  if TTable(FindComponent(QAgenda  + inttostr(I))).isEmpty
   then


btw, you should create a TDatamodule, and centralize all of your tables there

then this method would work great

for I:= 1 to 7 do
  if TTable(DataModule1.FindComponent(QAgenda  + inttostr(I))).isEmpty
   then


Shane
0
 

Author Comment

by:ROGERPORT
ID: 10902987
Your answer pointed me in the right direction, but still cant put it to work.

The QAgenda component is of type TADOQuery

Any idea why the code below gives an access violation run time error ?

    if not (TADOQuery(FindComponent('Form1.QAgenda' + inttostr(ct))).isEmpty) then
...

Though I recognise the value of you TDatamodule sugestion, is that mandatory ?

BTW where is that TDatamodule located ?  is that a component ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10903118
In your IDE, from the main menu, choose

new/ DataModule

A datamodule is a non-visible form where you can place all your non-visual dataset components (TTables, TDatasources, etc)


When you have created your new datamodule, save it, then do the following


from delphi ide main menu, choose

project / view source



you should have something like this

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {DataModule2: TDataModule};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TDataModule2, DataModule2);
  Application.Run;
end.


you need to move the Datamodule source up above the main form


program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {DataModule2: TDataModule};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TDataModule2, DataModule2);
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


This will make sure your main form is the last form created and therfore shown.


Then from each unit that needs access to the tables, you just add the datamodule unit to their source as follows:



unit Unit1; //main form

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2; //datamodule unit

{$R *.dfm}

end.


Shane


0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 500 total points
ID: 10903141
Sorry, I meant to add, adter you create your TDatamodule form, cut and past each of your Table, DataSource, & Query components from their current forms to the TDatamodule form, then save


Shane
0
 

Author Comment

by:ROGERPORT
ID: 10903200
Thanks for your valuable help. I'll change the way I am doing the application to use a datamodule.

BTW

I found what was causing the error.

must write:
if not (TADOQuery(Form1.FindComponent('QAgenda' + inttostr(ct))).isEmpty) then

insteed of:
if not (TADOQuery(FindComponent('Form1.QAgenda' + inttostr(ct))).isEmpty) then

Thanks again for your help
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10903249
Yeah, but if you place all the tables on a TDatamodule, then you wont have to deal with

the different forms

if Form1.QAgenda1.IsEmpty then
  ...
if Form2.QAgenda1.IsEmpty then
  ...
if Form3.QAgenda1.IsEmpty then
  ...
if Form4.QAgenda1.IsEmpty then
  ...
if Form5.QAgenda1.IsEmpty then
  ...
if Form6.QAgenda1.IsEmpty then
  ...
if Form7.QAgenda1.IsEmpty then


they will all be centralized on one form


SMILE

Shane
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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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