[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Approach a TTable by its name as a String

Posted on 2001-06-14
6
Medium Priority
?
241 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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

649 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