Board index » delphi » Help - Problems with CreateTable ?

Help - Problems with CreateTable ?

I am trying to create a table at runtime, using an existing table as a
template. I have followed the examples from D4 help and also form the manual
( page 20-18). However when executing 'CreateTable' I get and error 'Index
does not exist'. Can anyone please help ?

procedure CreateMyTable;
begin
     TempTable := TTable.Create( nil );
     T := TTable.Create( nil );
     TempTable.DatabaseName := 'TSW';
     TempTable.TableName := 'TEMPLATE.DB';
     T.DatabaseName := 'TSW';
     T.TableName := ExtractFileName( DBFileName );
     T.TableType := ttDefault;
     TempTable.FieldDefs.Update;
     { Add the fields }
     for i := 0 to TempTable.FieldDefs.count - 1 do
     begin
          T.FieldDefs.Add( TempTable.FieldDefs[ i ].Name,
          TempTable.FieldDefs[ i ].DataType,
          TempTable.FieldDefs[ i ].Size,
          TempTable.FieldDefs[ i ].Required );
     end;
     IdxOptions := [];
     { Add secondary indexes }
     T.IndexDefs.Clear;
     T.IndexDefs.Add( 'SeatIndex', 'FENTRANCE;FBLOCK;FROW;FSEATS',
IdxOptions );
     T.CreateTable; { produces error 'Index does not exist' }
     T.Free;
     TempTable.Free;
end;

The problem is in some way related to 'IndexDefs'. Removing the indexing
code creates a table as expected.

Michael Goode
mgo...@gresham-forms.co.uk

Many thanks in advance.

 

Re:Help - Problems with CreateTable ?


On Wed, 7 Oct 1998 16:51:13 +0100, "Mike Goode"

Quote
<mgo...@gresham-forms.co.uk> wrote:
>I am trying to create a table at runtime, using an existing table as a
>template. I have followed the examples from D4 help and also form the manual
>( page 20-18). However when executing 'CreateTable' I get and error 'Index
>does not exist'. Can anyone please help ?
[...]
>     TempTable.FieldDefs.Update;
>     { Add the fields }
>     for i := 0 to TempTable.FieldDefs.count - 1 do
>     begin
>          T.FieldDefs.Add( TempTable.FieldDefs[ i ].Name,
>          TempTable.FieldDefs[ i ].DataType,
>          TempTable.FieldDefs[ i ].Size,
>          TempTable.FieldDefs[ i ].Required );
>     end;

For one thing, if you are making an exact copy of the table you do not have
to iterate through the TFieldDef objects in the TTable.FieldDefs property.
You can simply use the TFieldDefs.Assign method. (Call TFieldDefs.Update
first, as you were already doing.) Something like this:

  if not Table1.Active then Table1.Open;
  Table1.FieldDefs.Update;
  Table2.FieldDefs.Assign(Table1.FieldDefs);
  Table2.CreateTable;

Of course, this has nothing to do with your original problem. But it is a
handy thing to know and can simplify your routine.

Quote
>     IdxOptions := [];
[...]
>     T.IndexDefs.Clear;
>     T.IndexDefs.Add( 'SeatIndex', 'FENTRANCE;FBLOCK;FROW;FSEATS',
>IdxOptions );

>The problem is in some way related to 'IndexDefs'. Removing the indexing
>code creates a table as expected.

Here is the root of your problem. You are creating a Paradox table and
attempting to add a secondary index without first having a primary index.
Such secondary indexes are non-maintained and Delphi does not support
non-maintained Paradox secondary indexes. To add this index, you would
first need to add a primary index (one where the Options parameter of the
TIndexDefs.Add method includes the TIndexOptions value ixPrimary).

//////////////////////////////////////////////////////////////////////////
Steve Koterski                 "What is success in this world? I would say
Technical Publications         it consists of four simple things: to live
INPRISE Corporation            a lot, to love a lot, to laugh a lot, and
http://www.inprise.com/delphi  from it all, to learn a lot."
                                                     -- Richard J. Needham

Re:Help - Problems with CreateTable ?


Quote
In article <6vg2bj$n...@forums.borland.com>, Mike Goode wrote:
> However when executing 'CreateTable' I get and error 'Index
> does not exist'

You have not created a primary index.

 Mike Orriss (m...@3kcc.co.uk)
 http://www.3kcc.co.uk/notetree.htm

Other Threads