Solved

Approach a TTable by its name as a String

Posted on 2001-06-14
6
238 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
[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
  • 4
6 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 6191951
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
ID: 6191992
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
ID: 6192054

You can do this too :

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

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

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 17

Expert Comment

by:geobul
ID: 6192063
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
ID: 6192549
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
ID: 6194262
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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