Board index » delphi » How to Duplicate Records with TTable Component

How to Duplicate Records with TTable Component

I have a form with some data aware components (~12) that the user can edit.
 I'm using Menu items - Save, Open, Save As, etc. to allow the user to
switch between and save changes to records through these components.  Save
(Post), Open (GotoKey) and New (Insert) work great for this purpose.  One
of the fields is 'QueryName' and is unique for each record.

The problem is Save As.  I want the new record to keep the User's input,
but in a new record with a new name.  basiaclly copy existing record or
data into new record, change the query name and then post change.

Insert creates an empty record and Edit dumps the previous "QueryName'.  

Any suggestions?  Maybe get rid of the data aware stuff and handle all of
the data passing  myself???

Help.

Denis Clavette

 

Re:How to Duplicate Records with TTable Component


You need to use Insert and assign a new QueryName, e.g.

table1.Insert;
table1 ['QueryName'] := 'Copy Of ' + oldQueryName;
// now prompt the user for a better QueryName

Quote
Shell Oil Company wrote in message <01be996e$1f13dbc0$aa4d398a@DP13733>...
>I have a form with some data aware components (~12) that the user can edit.
> I'm using Menu items - Save, Open, Save As, etc. to allow the user to
>switch between and save changes to records through these components.  Save
>(Post), Open (GotoKey) and New (Insert) work great for this purpose.  One
>of the fields is 'QueryName' and is unique for each record.

>The problem is Save As.  I want the new record to keep the User's input,
>but in a new record with a new name.  basiaclly copy existing record or
>data into new record, change the query name and then post change.

>Insert creates an empty record and Edit dumps the previous "QueryName'.

>Any suggestions?  Maybe get rid of the data aware stuff and handle all of
>the data passing  myself???

>Help.

>Denis Clavette

Re:How to Duplicate Records with TTable Component


Instantiate a second TTable and use that to do the insert on, then copy
the data from the first table field by field in code.

Assuming your main table object is called FirstTbl and positioned on a
valid record, some code like this may do the trick...

  with TTable.Create(nil) do begin
    try
      TableName:=FirstTbl.TableName;
      DatabaseName:=FirstTbl.DatabaseName;
      Open;
      Insert;
      For ix:=0 to FirstTbl.FieldCount-1 do begin
        Fields[ix].Value:=FirstTbl.Fields[ix].Value;
      end;
      FieldByName('QUERYNAME').AsString:='blah';{ Unique key field }
      Post;
    finally
      Free;
    end;
  end;

In article <01be996e$1f13dbc0$aa4d398a@DP13733>, Shell Oil Company
<dkclave...@shellus.com> writes

Quote
>I have a form with some data aware components (~12) that the user can edit.
> I'm using Menu items - Save, Open, Save As, etc. to allow the user to
>switch between and save changes to records through these components.  Save
>(Post), Open (GotoKey) and New (Insert) work great for this purpose.  One
>of the fields is 'QueryName' and is unique for each record.

>The problem is Save As.  I want the new record to keep the User's input,
>but in a new record with a new name.  basiaclly copy existing record or
>data into new record, change the query name and then post change.

>Insert creates an empty record and Edit dumps the previous "QueryName'.  

>Any suggestions?  Maybe get rid of the data aware stuff and handle all of
>the data passing  myself???

>Help.

>Denis Clavette

----------------------------
Charles Bainbridge

Re:How to Duplicate Records with TTable Component


Most of the corporate applications I write require a record duplication
feature. Here's how I do it:

procedure TMainForm.DupeButtonClick(Sender: TObject);
var
  aField : Variant ;
  i      : Integer ;
begin
  //Create a variant Array
  aField := VarArrayCreate(
               [0,TableName.Fieldcount-1],
                             VarVariant);

  // read values into the array
  for i := 0 to (TableName.Fieldcount-1) do begin
     aField[i] := TableName.fields[i].Value ;
  end;

 TableName.Insert;  // You could use APPEND here also.

  // Put array values into new the record
  for i := 0 to (TableName.Fieldcount-1) do begin
     TableName.fields[i].Value := aField[i] ;
  end;
end;

Paul C.

Quote
Shell Oil Company wrote:
> I have a form with some data aware components (~12) that the user can edit.
>  I'm using Menu items - Save, Open, Save As, etc. to allow the user to
> switch between and save changes to records through these components.  Save
> (Post), Open (GotoKey) and New (Insert) work great for this purpose.  One
> of the fields is 'QueryName' and is unique for each record.

> The problem is Save As.  I want the new record to keep the User's input,
> but in a new record with a new name.  basiaclly copy existing record or
> data into new record, change the query name and then post change.

> Insert creates an empty record and Edit dumps the previous "QueryName'.

> Any suggestions?  Maybe get rid of the data aware stuff and handle all of
> the data passing  myself???

> Help.

> Denis Clavette

Other Threads