Link to home
Start Free TrialLog in
Avatar of ROGERPORT
ROGERPORT

asked on

How to Evaluate a String Expression

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
Avatar of shaneholmes
shaneholmes

Something like this maybe

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


Shane
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
Avatar of ROGERPORT

ASKER

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 ?
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


ASKER CERTIFIED SOLUTION
Avatar of shaneholmes
shaneholmes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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