Board index » delphi » duplicate a record to an other record in the same table

duplicate a record to an other record in the same table

I am new in Delphi 3. I have a table with many fields for each record. I
want to duplicate a record to a new one in the same table.

I have to change my first field before appending because I have an index on
the first field ?

Please help with an exemple

Thank you

Yvon Labbee

 

Re:duplicate a record to an other record in the same table


The easiest way to do that is to declare variables for each of the fields in
the table, load the fields from the record you want to duplicate, call
append for the dataset and put the variables into the new record.

var
 Field1,field2,field3: String;

field1 := table1.FieldByName('YourField1').AsString;
field2 := table1.FieldByName('YourField2').AsString;
field3 := table1.FieldByName('YourField3').AsString;
Table1.Append;
table1.FieldByName('YourField1').AsString := Field1;
table1.FieldByName('YourField2').AsString := Field2;
table1.FieldByName('YourField3').AsString := Field3;
Table1.Post;

This is a very crude and simple example but it should give you some kind of
idea how to proceed.

--
Michael Glatz
mgl...@caiso.com

Quote
yvon labbee wrote in message <73h2nn$b...@forums.borland.com>...
>I am new in Delphi 3. I have a table with many fields for each record. I
>want to duplicate a record to a new one in the same table.

>I have to change my first field before appending because I have an index on
>the first field ?

>Please help with an exemple

>Thank you

>Yvon Labbee

Re:duplicate a record to an other record in the same table


Quote
yvon labbee wrote in message <73h2nn$b...@forums.borland.com>...
>I am new in Delphi 3. I have a table with many fields for each record. I
>want to duplicate a record to a new one in the same table.

>I have to change my first field before appending because I have an index on
>the first field ?

>Please help with an exemple

>Thank you

>Yvon Labbee

This depends on the database that you are using, this example is based on
Paradox tables.

You don't have to change the key value before you copy the record, you have
to change it before you post the new record to the table.

The following example has a couple of potential problems, for example if you
have used the fields editor to create calculated fields or modified which
fields are displayed then the ttable.fieldcount property will return a
different number of fields. It is clearly defined in the online help.

With the previous warning in mind, this procedure bypasses the need to
declare variables for each field and the resultant code required to place
the values back into your table.

This example creates a variant array with the same number of elements as the
table has fields. It then
iterates through the fields of the table and copies the contents of the
field to an array element.

It then appends a new record and iterates through the elements of the array,
copying all the array element values into the matching data field.

Note that you will need to change the key or indexed field value either with
code or through user input before you can post the new record to the
database or you will generate a key violation exception.

procedure TForm1.Button1Click(Sender: TObject);
var
  recordarray: variant; // declare variant array
  i: integer;                   // integer variable for looping
begin
// create variant array of with all elements declared as variant so can hold
// any data type

 recordarray := VarArrayCreate([0, table1.fieldcount], varVariant);

// because variant array indexes are zero based
// and we start our loop at zero we need to terminate
// our loop one less than the field count

// iterate the fields of the table copying the field values to the
// array elements

 for i:= 0 to table1.fieldcount -1 do
   recordarray[i] := table1.fields[i].value;

// append new record
 table1.append;
// iterate array elements copying element values to
// the data fields
 for i := 0 to table1.fieldcount-1 do
   table1.fields[i].value := recordarray[i];

// change key field value here by code or by user input
end;

As with any method or procedure someone gives you, you should take this
example apart and understand what it is doing before you use it. That way
you will understand its limitations and potential snares before they create
problems for you.

Wes

Re:duplicate a record to an other record in the same table


If the schemas are identical, you can do something like:

Table2.Edit;
for Counter := 0 to Table1.FieldCount - 1 do
        Table2.Fields[Counter].AsString := Table1.Fields[Counter].AsString;

.AsString works with NULLs; .Value does not. This won't work with blob
fields, but memos should be okay.

Cheers

Quote
yvon labbee wrote:

> I am new in Delphi 3. I have a table with many fields for each record. I
> want to duplicate a record to a new one in the same table.

> I have to change my first field before appending because I have an index on
> the first field ?

> Please help with an exemple

> Thank you

> Yvon Labbee

Re:duplicate a record to an other record in the same table


One way to copy a record is to use two TTable components, one pointing to
the old record and one pointing to the new record. Then use:

DestTbl.Insert;
for I from 0 to DestTable.FieldCount - 1 do
  DestTable.Fields[I].Assign(SourceTable.Fields[I]);
DestTbl.Post;

--
Bill Todd
(Sorry but TeamB cannot answer questions received via email)
(Remove nospam from my email address to contact me for any other reason)

Re:duplicate a record to an other record in the same table


Quote
> If the schemas are identical, you can do something like:

> Table2.Edit;
> for Counter := 0 to Table1.FieldCount - 1 do
>  Table2.Fields[Counter].AsString := Table1.Fields[Counter].AsString;

> .AsString works with NULLs; .Value does not. This won't work with blob
> fields, but memos should be okay.

I'm interested in this too.

How about:
  Table2.Fields[Counter].Assign (Table1.Fields[Counter]);

I saw this somewhere (not sure where) - will it work with NULLs and Blobs?

Dave Riley
Tangent Software Ltd, Reading, UK

Other Threads