Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Approach a TTable by its name as a String

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
bassch
Asked:
bassch
  • 4
1 Solution
 
geobulCommented:
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
 
geobulCommented:
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
 
DraregCommented:

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.

 
geobulCommented:
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
 
basschAuthor Commented:
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
 
geobulCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now