Creating TTable at run-time

I would like to create a TTable object at run-time and I wrote this code in a unit, but this exception occurs during run : << Project OliForni.exe raise exception class EDBEngineError with massage 'Table does not exist. File or directory does not exist. File : E:\Delphi4\Exe\itamsg.db'. >>. In effect, the table file is not in the current directory but I don't know why the program hangs; I set the "active" property to false, it means do not open the file. I have the same problem if I use the TTable component in the VCL : if the file does not exist (or it's in another directory) the program raise this exception.
I need to check later if the table exists (I think with "CheckOpen" property).

Regards,
Gianluca Bonfatti
ITALY

Unit DefMod;
...
var
 TabLingua:TTable;
...
initialization

TabLingua:=TTable.create(application);
TabLingua.active:=false;
TabLingua.tablename:='itamsg.db';
TabLingua.exclusive:=false;
TabLingua.tabletype:=ttParadox;
TabLingua.readonly:=true;

finalization

TabLingua.free;
...
end.
gianlucaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
Hi Gianluca,

you need to set

TabLingua.DatabaseName := 'C:\Your Path\';

before activating the table.

Ciao, Mike
0
LischkeCommented:
Oops, sorry, answered too fast :-) You cannot open a table if it doesn't exist.

To create a new physical table you need to set up a TTable class and fill in all information in either FieldDefs or Fields. Then you can call TTable.CreateTable.

Ciao, Mike
0
kretzschmarCommented:
hi  gianluca,

have tested your code, the error doesn't appear by me!
seems that you open somewhere the table, or the error comes from another TTable (check your code).

one remark, you may not free the table, because it is freed by the owner (application in this case)

this line
  TabLingua.free;

causes an access violation, because the table is freed by application.

meikl
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Fatman121898Commented:
Chiao, Gianluca!

Here is a simple part of code I've used in my app.
All you need to do is to define fields for the table
before calling CreateTable method.

procedure CreateUserTable;
  var
    CT: TTable;
  begin
    FCBFspec:='C:\Temp\+BrTableFName;          
    CT:=TTable.Create(nil);
    CT.TableName:=FCFspec;
    CT.FieldDefs.Add('NAME1', ftString, 8, True);
    CT.FieldDefs.Add('Name2', ftString, 32, True);
    CT.FieldDefs.Add('Counter1', ftInteger, 0, True);
    CT.FieldDefs.Add('Counter2', ftInteger, 0, True);
    CT.CreateTable;
    CT.Open;

    //work with CT

    CT.Close;
    CT.Free;
  end;


Jo.
0
Fatman121898Commented:
Oops - misprint:
after "begin" you should write:
FCBFspec:='C:Temp\'+BrTableFName;
I ve missed a quote in path string.

Jo.
0
gianlucaAuthor Commented:
Your reply is correct but I formulated wrong the question. I need to OPEN (not create) a table and I suppose I have to create a TTable object to access to it; then I would like to check if the table exist or not.
0
LischkeCommented:
As I already said. You cannot open a table which doesn't exist. But you can put the open command into a try except block like this:

function OpenTable(Table: TTable): Boolean;

begin
  try
    Table.Open;
    Result := True;
  except
    Result := False;
  end;
end;

This will try to open the table and return True when successful otherwise False. Because there's the try/except block no exception is shown.

Ciao, Mike
0
Fatman121898Commented:
Lischke is right.
0
MotazCommented:
You can check database file existance using FileExists function e.g.

if FileExists('c:\data\phone.db') then
  begin
  Table1.TableName:= 'c:\data\phone.db';
  Table1.Open;
....
  end;

Motaz
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LischkeCommented:
I should have mentioned the obvious first. gianluca, I thought you know FileExists and cannot use it for some reason. Lost time :-/

Ciao, Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.