Cloning Records In Same Table

I would like to know what is the easiest way to copy a record in a table.  Let me give you the specifics....

1)  Useing DBase as the backend.
2)  Would like to copy a specific record that displays on  a form that is called to display all the copied data except the unique identifier.  Also, don't want the record to post until the users verifies to save it.

More specific example.
Application is displaying a guest information record with the guest id being unique.  User hits a button to clone record and a new guest information form is called displaying all the same (copied) data except the unique guest id, which the user must then input (they can also change edit any of the other data fields) User then can either save the new (copied) record or abort it (not post it).  

I looked at using the batch move component and possibly creating a record or array, but need to know if I'm missing something or if I'm heading in the right direction with one of the approaches I've listed above.

Thanks,
GG
GGriffithAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi GGriffith,

here is a better one

procedure TForm1.Button2Click(Sender: TObject);
var
  T : TTable;
  i : Integer;
begin
  T := TTable.Create(Application);
  try
    T.DatabaseName :=  Table1.DatabaseName;
    T.TableName :=  Table1.TableName;
    T.Open;
    T.GotoCurrent(Table1);
    Table1.Append;
    for i := 1 to t.FieldCount - 1 do   //Field 0 is the ID leave empty
      table1.Fields[i].Assign(t.Fields[i]);
    T.Close;
  finally
    t.free;
  end;
end;

meikl
0
 
kretzschmarCommented:
hi GGriffith,

you can easy do it with an additional table-component
like this sample
users select in dbgrid1 and copy by buttonpress the record to dbgrid2 as new record both dbgrids are pointed to the same physical table.

unit rec_copy_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, ExtCtrls, DBCtrls, Db, DBTables, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Table1: TTable;  //with table2 pointed to the same table
    Table2: TTable;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DBNavigator1: TDBNavigator;
    DBNavigator2: TDBNavigator;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//insert record in table2 and copy fields
//from current record from table1
procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
  Table2.append;
  for i := 1 to table1.FieldCount - 1 do   //Field 0 is the ID leave empty
    table2.Fields[i].Assign(table1.Fields[i]);
end;

end.

try it out

meikl
0
 
GGriffithAuthor Commented:
I'm trying to get this information from say a guest information form to a new instance of the guest information form....(same form with virtually same info)...Using your choices it doesn't seem to come across....Any ideas?? Thanks a bunch....
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
kretzschmarCommented:
hi GGriffith,

you must explain more,
does this mean you have an app like a mdi-app?
do you use a datamodule or are the ttable(s), query(s) on the form?

meikl
0
 
kretzschmarCommented:
hi GGriffith,

here a sample of an mdi-child, which creates itselfs, and insert a new record in the new created form and copys there the fields of the self current-record.

unit mdi_rec_copy_c_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, Db, DBTables;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
var
  f : TForm2;
  I : Integer;
begin
  F := TForm2.Create(Application);  //Create itself
  F.Table1.Open;                    //Open the table
  F.Table1.Append;                  //prepare for insert
  For i := 1 to Table1.FieldCount - 1 do  //Copy fields except id
    F.Table1.Fields[I].Assign(Table1.Fields[i]);
  F.Show;                           //Show the new Form
end;

end.

meikl
0
 
kretzschmarCommented:
hi GGriffith,
whats going on?
meikl
0
 
GGriffithAuthor Commented:
Sorry had to go out of town...
Will try it today or tomorrow and let you know....
Thank you...

0
 
GGriffithAuthor Commented:
Thank you very much.....
0
All Courses

From novice to tech pro — start learning today.