• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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.
0
gianluca
Asked:
gianluca
1 Solution
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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
 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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