Solved

Approach a TTable by its name as a String

Posted on 2001-06-14
6
232 Views
Last Modified: 2010-05-18
Hi All,

I have a set of strings that have the names to different TTables. ('tblCars' or 'tblCycles' as strings).

What i want to do in a simple way, is to approach a table as <name of the table>.FieldByName('Brand').AsString;

Which table is to be used is returned by another query (I pass the table name thrue as a string).

How can you get the correct table if you only have its name as a string?


Greetings,

Bas
0
Comment
Question by:bassch
  • 4
6 Comments
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Hi,
Place a TTable component on your form and set its DatabaseName property.

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Table1 do begin
    Close;
    TableName := 'clients.dbf';
    Open;
    Label1.Caption := FiledByName('LAST_NAME').AsString;
    Close;
  end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
or even better:

function TForm1.ReadAField(TName, FName: string): string;
begin
  result := '';
  try
    with Table1 do begin
      Close;
      TableName := TName;
      Open;
      result := FieldByName(FName).AsString;
    end;
  finally
    Table1.Close;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := ReadAField('clients.dbf', 'LAST_NAME');
end;

Regards, Geo
0
 
LVL 2

Accepted Solution

by:
Drareg earned 300 total points
Comment Utility

You can do this too :

  TTable(FindComponent(<name of table>)).FieldByName('Brand').AsString

Ex.:  If TTable(FindComponent('TableName')).Active Then...

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 17

Expert Comment

by:geobul
Comment Utility
Hi again,

For already opened TTable use this:

function TForm1.ReadAField(TName, FName: string): string;
var
  i: integer;
begin
  result := '';
  for i := 0 to Form1.ComponentCount - 1 do
    if Form1.Components[i] is TTable then
      with TTable(Form1.Components[i]) do
        if TableName = TName then
          result := FieldByName(FName).AsString;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := ReadAField('CLIENTS.DBF', 'LAST_NAME');
end;

Regards, Geo
0
 

Author Comment

by:bassch
Comment Utility
Geo,

By using your method I can no longer use my tables that are already there in my datamodule.

If I had one template TTable than this would be a good option..


Drareg,

This was the line I was looking for.. thanks. I new the FindComponent method, but its funny how fast you overlook some stuff:)




0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Hi Bas,
I thought that 'tblCars' or 'tblCycles' were the names of the real tables in the db, not of your ttable components. Glad that you found what you were looking for.

Regards, Geo
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

11 Experts available now in Live!

Get 1:1 Help Now