Board index » delphi » Runtime creation of secondary index for paradox table

Runtime creation of secondary index for paradox table

Hi,
How do I go about creating a secondary index for a table at runtime?  My
current procedure looks like this but I need a secondary index to sort
on a different field, say 'surname'.

procedure CreateContactList;
var
  NewTable : TTable;
  NewIndexOption : TIndexOptions;
begin
  NewTable := TTable.Create(Application);
  NewIndexOptions := [ixPrimary, ixUnique];
  with NewTable do
  begin
    Active := false;
    Databasename := LLDatapath;
    TableName := TempContactList;
    TableType := ttDefault;
    FieldDefs.clear;
    FieldDefs.add('ContactID',ftAutoInc,0,True);
    FieldDefs.add('ShortName',ftString,10,False);
    FieldDefs.add('Title',ftString,20,False);
    FieldDefs.add('Surname',ftString,20,False);
    FieldDefs.add('Name',ftString,20,False);
    IndexDefs.clear;
    IndexDefs.add('Primary Index', 'ContactID', NewIndexOptions);
  end;  // with NewTable
  NewTable.CreateTable; { create the table}
end;

thanks
Bennie

 

Re:Runtime creation of secondary index for paradox table


Use the TTable.CreateIndex method to create a secondary index.

Bill

--

Bill Todd - TeamB
(TeamB cannot respond to email questions. To contact me
 for any other reason remove nospam from my address.)

Re:Runtime creation of secondary index for paradox table


Quote
>Bennie Coetzer wrote:

>IndexDefs.clear;
>IndexDefs.add('Primary Index', 'ContactID', NewIndexOptions);

Just add another one:

  IndexDefs.Add('IdxSur', 'surname', [indexoptions]);

To add an index to an existing table, use

  Mytable.AddIndex('IdxSur', 'surname', [indexoptions]);

and with SQL  
  ..
  Query1.SQL.Add('CREATE INDEX IdxSur ON "Mytable.db" (surname)');
  ..
  Query1.ExecSQL;

BTW, no need to define NewIndexOptions as you'll use this particular
combination [ixPrimary, ixUnique] only once unless you want to build
a composite key.
_________
Andreas

Re:Runtime creation of secondary index for paradox table


Quote
Bennie Coetzer <benn...@thales.co.za> wrote:
>How do I go about creating a secondary index for a table at runtime?  My
>current procedure looks like this but I need a secondary index to sort
>on a different field, say 'surname'.

>procedure CreateContactList;
>var
>  NewTable : TTable;
>  NewIndexOption : TIndexOptions;
>begin
>  NewTable := TTable.Create(Application);
>  NewIndexOptions := [ixPrimary, ixUnique];
>  with NewTable do
>  begin
>    Active := false;
>    Databasename := LLDatapath;
>    TableName := TempContactList;
>    TableType := ttDefault;
>    FieldDefs.clear;
>    FieldDefs.add('ContactID',ftAutoInc,0,True);
>    FieldDefs.add('ShortName',ftString,10,False);
>    FieldDefs.add('Title',ftString,20,False);
>    FieldDefs.add('Surname',ftString,20,False);
>    FieldDefs.add('Name',ftString,20,False);
>    IndexDefs.clear;

    // Paradox primary index has no name
    IndexDefs.Add('', 'ContactID', NewIndexOptions);
    IndexDefs.Add('SNAMEIDX', 'Surname', [ixCaseInsensitive]);

Quote
>  end;  // with NewTable
>  NewTable.CreateTable; { create the table}
>end;

Regards,
Chris Roberts

Other Threads