Title: How to copy/move one Table-Record with an Autoinc-field
Question: Your Table has an AutoInc-Field and you must copy Records from this Table to an Duplicate-Table with the same structure.
Answer:
AutoInc-Fields can't be copied they must be skipped and they becomes automaticly own values in the Outputtable.
To Test it you need this Objects
(use Duplicates from your Tables):
TForm Form1
TTable Table1 (connected to a Table with AutoInc-Field)
TTable Table2 (connected to a Duplicate from Table1)
TDataSource DataSource1 (connectet to Table1)
TDataSource DataSource2 (connectet to Table2)
TDBGrid DBGrid1 (connected to DataSource1)
TDBGrid DBGrid2 (connected to DataSource2)
TButton Button1 (with onClick Button1Click - 'Copy')
TButton Button2 (with onClick Button2Click - 'Move')
// Here the Copy-Procedure
// (If Your Table comes from a Query then use TQuery as Parameter:
// procedure CopyRecordQ(ST: TQuery; DT: TTable);)
procedure CopyRecord(ST, DT: TTable);
var
I: Integer;
begin
DT.Append;
// process all fields
for I := 0 to DT.FieldCount - 1 do
// if the Field-Datatype ftAutoinc then skip this Field
if not (ST.Fields[I].DataType = ftAutoInc) then
// It's a 'normal' Field then Copy its value
DT.FindField(ST.Fields[I].FieldName).Assign(ST.FieldByName(ST.Fields[I].FieldName));
DT.Post;
end;
// here the CopyButton
procedure TForm1.Button1Click(Sender: TObject);
begin
CopyRecord(Table1, Table2);
end;
// and here the MoveButton
procedure TForm1.Button2Click(Sender: TObject);
begin
CopyRecord(Table1, Table2);
Table1.Delete;
end;
// open the Tables
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.open;
Table2.Open;
end;
// and close the Tables
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
Table1.close;
Table2.First;
Table2.close;
end;