Solved

How to Evaluate a String Expression

Posted on 2004-04-23
7
427 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now