Use a second TTable component connected to the same database table and then append or insert a
record from the current record in the first table into the second table.
eg.
Table2.AppendRecord([Table1.Fields[0],Table1.Fields[1],Table1.Fields[2],Table1.Fields[n]);
I think the easiest way is:
var
iCount : LongInt;
sName : String;
begin
Table2.Insert;
for iCount:=0 to Table2.FieldCount-1 do
begin
sName := Table2.Fields[iCount].FieldName;
if (Table1.FindField(sName)<>nil) and (sName<>'ID') then
Table2.FieldByName(sName).Assign(Table1.FieldByName(sName));
end;
Table2.Post;
end;
If you work with FieldByName, there are 2 advantages:
You can copy only the fields you want. In the upper example, the field
"ID" would not be copied.
The construction of the two tables must not the same. Only fields with
the same name would be copied.
If you are sure, the construction is the same
for iCount:=0 to Table2.FieldCount-1 do
Table2.Fields[iCount].Assign(Table1.Fields[iCount]);
also works.
-Jeff Hamblin
Paulus Gerd <07762...@T-Online.de> wrote in article
<34581D...@T-Online.de>...
type
ChangedRecord = record
Field1: String[8];
Field2: Array[0..11] of Char;
Field3: Double;
end;
Then:
with ChangedRecord do
begin
Field1:= Table1.FieldByName('Date').AsString;
Field2:= Table1.FieldByName('Company').AsString;
Field3:= Table1.FieldByName('NumOfWidgets').AsInteger;
end;
with Table1 do
begin
Insert;
Table1.FieldByName('Date').AsString:= Field1;
Table1.FieldByName('Company').AsString:= Field2;
Table1.FieldByName('NumOfWidgets').AsInteger:= 203945;
Post;
end;
>I need to be able to copy/almost duplicate the current record in a TTable
>(Paradox 5.0 Table).It must be dead simple or so I thought 24 hrs ago
Give this a try
function CopyRecord(Table:TTable):boolean; {from Bill Todd}
var
buf:PChar;
begin
GetMem(buf,Table.TRecordSize);
try
Table.GetCurrentRecord(buf);
Result := DBIInsertRecord(Table.DBHandle,dbNoLock, Buf);
finally
FreeMem(buf);
end;
end;
--
Brian Bushay (TeamB)
Bbu...@DataGuidance.com
place original table in insert mode, iterate through tempory table
for i := 0 to TempTable.FieldCount -1 do begin
OriginalTable.FieldbyName(TempTable.Fields[i]).Assign(TempTable.Fields[i]);
end;
Caveats..
You need the "FieldByName" syntax as you cannot assume that the
field order of the two datasets is the same.
This will try and copy all fields. One exception may be raised
during execution if you try to assign to an autoinc field, onother will
be raised at post because you will have duplicate keys.
to deal with this I normally use GetFieldNames and delete the names of
fields that I dont want copied and then use this loop
for i := 0 to MyFieldList.Count -1 do begin
OriginalTable.FieldbyName(MyFieldList[i]).Assign
(TempTable.FieldByName(MyFieldList[i]));
end;
This works but is not exactly abstract.
I'd love to here a better way myself.
Hope this helps...
Steve Griffiths.
John Olliver wrote:
> I need to be able to copy/almost duplicate the current record in a
> TTable
> (Paradox 5.0 Table).It must be dead simple or so I thought 24 hrs ago
>