Solved

How to Evaluate a String Expression

Posted on 2004-04-23
7
432 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
[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
  • 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
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!

 
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 125 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 180
tidtcpserver connection lost handle 2 119
Unique identifier on a terminal server (rdp) 4 81
Typecasting TBytes to Integer in Delphi XE8 2 58
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

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