Board index » delphi » How to copy/duplicate a record in a TTable

How to copy/duplicate a record in a TTable

How to copy/duplicate a record in a TTable How to copy/duplicate a record in a TTable How to copy/duplicate a single record in TTable
Date: 1997/10/29
Message-ID: <637vav$mo64@forums.borland.com>#1/1
X-Deja-AN: 285841608
Organization: None
Newsgroups: borland.public.delphi.database.desktop

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

Amongst other things I have tried:
----------------------------------------
for x:=0 to Table1.fieldcount-1 do
begin
with Table1.fields[x] do
   begin
   GetMem(Mybuffer,datasize);
   GetData(buffer);
   MyArray[x]:=(buffer);
   end;
end;
Table1.Edit;
Table1.Setfields(MyArray) ;
-----------------------------------------
I understand that Setfields takes an Array of Const but I can?t seem to declare
MyArray as an Array of Const. If I declare it as an array of String (say) then
Setfields {*word*88}s on it. And if I could declare MyArray as an array of const how
would I pass the field data into it. Can I pass the (buffer) from
GetData(buffer,datasize) into MyArray ? (And what is the meaning of life.)

Or perhaps there?s a completely better way. Now spent over a day on this and
nothing seems to work so any help much appreciated.

John Olliver

 

Re:How to copy/duplicate a record in a TTable


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

Quote
>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

>Amongst other things I have tried:
>----------------------------------------
>for x:=0 to Table1.fieldcount-1 do
>begin
>with Table1.fields[x] do
>   begin
>   GetMem(Mybuffer,datasize);
>   GetData(buffer);
>   MyArray[x]:=(buffer);
>   end;
>end;
>Table1.Edit;
>Table1.Setfields(MyArray) ;
>-----------------------------------------
>I understand that Setfields takes an Array of Const but I can?t seem to declare
>MyArray as an Array of Const. If I declare it as an array of String (say) then
>Setfields {*word*88}s on it. And if I could declare MyArray as an array of const how
>would I pass the field data into it. Can I pass the (buffer) from
>GetData(buffer,datasize) into MyArray ? (And what is the meaning of life.)

>Or perhaps there?s a completely better way. Now spent over a day on this and
>nothing seems to work so any help much appreciated.

>John Olliver

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

Re:How to copy/duplicate a record in a TTable


Quote
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

> Amongst other things I have tried:
> ----------------------------------------
> for x:=0 to Table1.fieldcount-1 do
> begin
> with Table1.fields[x] do
>    begin
>    GetMem(Mybuffer,datasize);
>    GetData(buffer);
>    MyArray[x]:=(buffer);
>    end;
> end;
> Table1.Edit;
> Table1.Setfields(MyArray) ;
> -----------------------------------------
> I understand that Setfields takes an Array of Const but I can?t seem to declare
> MyArray as an Array of Const. If I declare it as an array of String (say) then
> Setfields {*word*88}s on it. And if I could declare MyArray as an array of const how
> would I pass the field data into it. Can I pass the (buffer) from
> GetData(buffer,datasize) into MyArray ? (And what is the meaning of life.)

> Or perhaps there?s a completely better way. Now spent over a day on this and
> nothing seems to work so any help much appreciated.

> John Olliver

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.

Re:How to copy/duplicate a record in a TTable


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 <07762766...@T-Online.de> wrote in article
<34581D73....@T-Online.de>...

Quote
> 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

> > Amongst other things I have tried:
> > ----------------------------------------
> > for x:=0 to Table1.fieldcount-1 do
> > begin
> > with Table1.fields[x] do
> >    begin
> >    GetMem(Mybuffer,datasize);
> >    GetData(buffer);
> >    MyArray[x]:=(buffer);
> >    end;
> > end;
> > Table1.Edit;
> > Table1.Setfields(MyArray) ;
> > -----------------------------------------
> > I understand that Setfields takes an Array of Const but I can?t seem to
declare
> > MyArray as an Array of Const. If I declare it as an array of String
(say) then
> > Setfields {*word*88}s on it. And if I could declare MyArray as an array of
const how
> > would I pass the field data into it. Can I pass the (buffer) from
> > GetData(buffer,datasize) into MyArray ? (And what is the meaning of
life.)

> > Or perhaps there?s a completely better way. Now spent over a day on
this and
> > nothing seems to work so any help much appreciated.

> > John Olliver

> 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.

Re:How to copy/duplicate a record in a TTable


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;

Re:How to copy/duplicate a record in a TTable


Quote
>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)
Bbus...@DataGuidance.com

Re:How to copy/duplicate a record in a TTable


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.

Quote
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

> Amongst other things I have tried:
> ----------------------------------------
> for x:=0 to Table1.fieldcount-1 do
> begin
> with Table1.fields[x] do
>    begin
>    GetMem(Mybuffer,datasize);
>    GetData(buffer);
>    MyArray[x]:=(buffer);
>    end;
> end;
> Table1.Edit;
> Table1.Setfields(MyArray) ;
> -----------------------------------------
> I understand that Setfields takes an Array of Const but I can?t seem
> to declare
> MyArray as an Array of Const. If I declare it as an array of String
> (say) then
> Setfields {*word*88}s on it. And if I could declare MyArray as an array of
> const how
> would I pass the field data into it. Can I pass the (buffer) from
> GetData(buffer,datasize) into MyArray ? (And what is the meaning of
> life.)

> Or perhaps there?s a completely better way. Now spent over a day on
> this and
> nothing seems to work so any help much appreciated.

> John Olliver

Other Threads