Board index » delphi » AppendRecord Problem with Autoincrement Key Fields

AppendRecord Problem with Autoincrement Key Fields

Anyone That Can Help;

This is the code:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, Db, DBTables, StdCtrls;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Table1: TTable;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
  begin
    with Table1 do
    begin
      ReadOnly := False;
      Open;
      //-- EDatabaseError created by following line
      AppendRecord( [ Null, Edit1.Text ] );
      Close;
      ReadOnly := True;
    end;
  end;
end.

//-- the Paradox 7 table definition for FORUMS.DB is:

Field Name      Type    Size    Key     Required        Default
+ FORUMID       AutoInc         *       Y               Auto
A FORUMNAME     Alpha   32              N              

//-- and here is the error I get when it runs:
"Project Project1.exe raised exception class EDatabaseError with message
'Field 'FORUMID' cannot be modified".

If I remove the line with AppendRecord() in it and replace it with
the following three lines, everything works !!

      Append;
      FieldValues['FORUMNAME'] := Edit1.text;  // works
      Post;

The interesting thing is that if the code is compiled under Delphi3, it
works !

Does anyone have any suggestions about using AppendRecord() under these
conditions ?  In the Delphi4 help topics, this is the definition of
AppendRecord:

"This statement appends a record to the Customer table. Note that Nulls
are entered for some of the values, but are not required for missing
values at the end of the array argument, i.e. after the Discount field.

Customer.AppendRecord([CustNoEdit.Text,CoNameEdit.Text, AddrEdit.Text,
Null, Null, Null, Null, Null, Null, DiscountEdit.Text]);"

Richard Bateman
rdbate...@bluecrow.com

 

Re:AppendRecord Problem with Autoincrement Key Fields


Quote
Richard Bateman wrote:
> SNIP
>       //-- EDatabaseError created by following line
>       AppendRecord( [ Null, Edit1.Text ] );
> SNIP
> //-- the Paradox 7 table definition for FORUMS.DB is:

> Field Name      Type    Size    Key     Required        Default
> + FORUMID       AutoInc         *       Y               Auto
> A FORUMNAME     Alpha   32              N

> //-- and here is the error I get when it runs:
> "Project Project1.exe raised exception class EDatabaseError with message
> 'Field 'FORUMID' cannot be modified".

You can't give a value to the autoincrement field. This is how it is
supposed to be so if it works on D3 it's kind of a bug.

BTW. I recommend you use append and then give values to specific fields
rather than depend on the order the fields (table structures can then be
modified). Or use SQL (as I always do).

--
Henri Suuronen

Other Threads