?
Solved

Cloning Records In Same Table

Posted on 1999-08-02
8
Medium Priority
?
157 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:GGriffith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392450
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 400 total points
ID: 1392451
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
 

Author Comment

by:GGriffith
ID: 1392452
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392453
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392454
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1392455
hi GGriffith,
whats going on?
meikl
0
 

Author Comment

by:GGriffith
ID: 1392456
Sorry had to go out of town...
Will try it today or tomorrow and let you know....
Thank you...

0
 

Author Comment

by:GGriffith
ID: 1392457
Thank you very much.....
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question