?
Solved

How to Evaluate a String Expression

Posted on 2004-04-23
7
Medium Priority
?
435 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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