Board index » delphi » ADOX Composite Primary Key Problem

ADOX Composite Primary Key Problem

I am using ADOX in Delphi 6 to create tables in MDB files. The following
statement works when the primary key consists of a single column 'KEY1'

Table.Keys.Append( 'PRIMARY KEY', adKeyPrimary, 'KEY1', '', '' );

But I couldn't make it work when the primary key is a composite key with
three columns: 'KEY1','KEY2' and 'KEY3'. I think the code should look
something like the following:

var
  Catalog : _Catalog;
  Table : _Table;
   Key : _Key;
begin
  //The table is created and a number of columns, including 'KEY1', 'KEY2'
and 'KEY3' are added...

  Key := CoKey.Create;
  Key.Name  := 'PrimaryKey';
  Key.Type_ := adKeyPrimary;
  Key.Columns.Append( 'KEY1', adInteger, 0 );
  Key.Columns.Append( 'KEY2', adInteger, 0 );
  Key.Columns.Append( 'KEY3', adInteger, 0 );
  Table.Keys.Append( Key, 0, '', '', '' );    //The program stops here
  Catalog.Tables.Append( Table );   //Attache table to catalog

The program raises an exception when it executes the Table.Keys.Append
statement, complaining that one or more parameters are either of the wrong
type or out of range. I have tried a lot of different variations, but
nothing worked.

Please help before I pull all my hair out!

Sherman

 

Re:ADOX Composite Primary Key Problem


Hi Sherman,

Quote
> But I couldn't make it work when the primary key is a composite key with
> three columns: 'KEY1','KEY2' and 'KEY3'.

This should help you:
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=15292

Thrse

Re:ADOX Composite Primary Key Problem


Quote
"Sherman Chan" <sc...@aspeninc.com> wrote in message

news:3d0fb4c8$1_1@dnews...

Quote
> I am using ADOX in Delphi 6 to create tables in MDB files. The following
> statement works when the primary key consists of a single column 'KEY1'

> Table.Keys.Append( 'PRIMARY KEY', adKeyPrimary, 'KEY1', '', '' );

> But I couldn't make it work when the primary key is a composite key with
> three columns: 'KEY1','KEY2' and 'KEY3'. I think the code should look
> something like the following:

> var
>   Catalog : _Catalog;
>   Table : _Table;
>    Key : _Key;
> begin
>   //The table is created and a number of columns, including 'KEY1', 'KEY2'
> and 'KEY3' are added...

>   Key := CoKey.Create;
>   Key.Name  := 'PrimaryKey';
>   Key.Type_ := adKeyPrimary;
>   Key.Columns.Append( 'KEY1', adInteger, 0 );
>   Key.Columns.Append( 'KEY2', adInteger, 0 );
>   Key.Columns.Append( 'KEY3', adInteger, 0 );
>   Table.Keys.Append( Key, 0, '', '', '' );    //The program stops here
>   Catalog.Tables.Append( Table );   //Attache table to catalog

> The program raises an exception when it executes the Table.Keys.Append
> statement, complaining that one or more parameters are either of the wrong
> type or out of range. I have tried a lot of different variations, but
> nothing worked.

From http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=15292

  procedure AppendForeignKeysToTables;
  begin
    // Append the foreign key.
    // Note carefully the use of Unassigned and EmptyParam.  This is not
    // arbitrary.
    DTable.Keys.Append(DFKey, Unassigned, EmptyParam, Unassigned,
Unassigned);
  end;

--

Quidquid latine dictum sit, altum videtur.

Re:ADOX Composite Primary Key Problem


Quote
>But I couldn't make it work when the primary key is a composite key with
>three columns: 'KEY1','KEY2' and 'KEY3'. I think the code should look
>something like the following:

Try SQL

ALTER TABLE bob
   ADD CONSTRAINT PrimaryKey Primary key
   (key1,key2,key3)

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads