Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to copy/duplicate a record in a TTable

1,583 views
Skip to first unread message

Warren Ashman

unread,
Oct 29, 1997, 3:00:00 AM10/29/97
to

In article <637vav$mo...@forums.borland.com>,
John Olliver <joll...@enterprise.net> wrote:

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]);

Paulus Gerd

unread,
Oct 29, 1997, 3:00:00 AM10/29/97
to


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

unread,
Oct 29, 1997, 3:00:00 AM10/29/97
to

If I'm not mistaken, I think the original poster wants to duplicate(except
key field) a record within the SAME table, not copy it to another. For
example a repeat invoice or the like. Does that mean your Table1 and Table2
use the same DB Table file?

-Jeff Hamblin

Paulus Gerd <07762...@T-Online.de> wrote in article
<34581D...@T-Online.de>...

Timothy J. Kelly

unread,
Oct 29, 1997, 3:00:00 AM10/29/97
to

Why don't you declare a record that matches your database layout:

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;

Brian Bushay TeamB

unread,
Oct 30, 1997, 3:00:00 AM10/30/97
to

>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

Steve Griffiths

unread,
Oct 30, 1997, 3:00:00 AM10/30/97
to

Create a table object, point it to the original table, and sychronize
(GoToCurrent)

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
>

0 new messages