Board index » cppbuilder » How do I copy a record in TTable?

How do I copy a record in TTable?

I have a TTable in which I want to make a duplicate record of an existing
record.  I have a unique index, so it can't be an exact duplicate.  I want
to change 1 field first before posting.

Is there an easy way to do this, or do I have to create two TTables which
point to the same data and do  the following  (which would be a ROYAL pain
with 400+ columns!):

Table2->FieldValues["fieldX"] = Table1->FieldValues["fieldX"];
Table2->FieldValues["fieldY"] = Table1->FieldValues["fieldY"];
Table2->FieldValues["fieldZ"] = Table1->FieldValues["fieldZ"];
.
.// do this for 400+ fields!
.
.
Table1->Post();

--
Lance Robaldo
La...@Robaldo.com

 

Re:How do I copy a record in TTable?


you can store all the field values in an array of Varients then Insert into
your table copy the values back and change the one field you want changed.

You can also use GetRecord which will fill a app supplied buffer with the
current record.

Quote
"Lance Robaldo" <La...@Robaldo.com> wrote in message

news:3a855d8e$1_2@dnews...
Quote
> I have a TTable in which I want to make a duplicate record of an existing
> record.  I have a unique index, so it can't be an exact duplicate.  I want
> to change 1 field first before posting.

> Is there an easy way to do this, or do I have to create two TTables which
> point to the same data and do  the following  (which would be a ROYAL pain
> with 400+ columns!):

> Table2->FieldValues["fieldX"] = Table1->FieldValues["fieldX"];
> Table2->FieldValues["fieldY"] = Table1->FieldValues["fieldY"];
> Table2->FieldValues["fieldZ"] = Table1->FieldValues["fieldZ"];
> .
> .// do this for 400+ fields!
> .
> .
> Table1->Post();

> --
> Lance Robaldo
> La...@Robaldo.com

Re:How do I copy a record in TTable?


Another option is to use a for loop to unload/reload.

Put a second TTable object on your form defined like the first including the
TableName property.
Position the first table on the record you want to copy.
Insert a new record into the second table.
Then:
    for (int i = 0; i < table1->FieldCount; ++i)
        table2->Fields->Fields[i]->Value = table1->Fields->Fields[i]->Value;
Make your key changes (or whatever).
Post the new table2 record.

The key to this is that you can have multiple TTable components for a single
underlying physical table.

HTH
Mark

Other Threads