BCS Delphi XE8 Clone A Record Code Generator


This code snippet will generate the source code needed to clone a new record in a data table from an existing record.
dbs

procedure TBCSResumedm.car(ta: TUniTable);
var
  i: Integer;
  buf: String;
  sc: TStringList;
begin
  sc := TStringList.Create;
  sc.Add('  InsertRecord ' + ta.Name +'([');
  i := 0;
  repeat
    buf := '    ' + ta.Name + '.FieldByName(' + '''' + ta.Fields[i].FullName +
      '''' + ').AsString,';
    if i = ta.Fields.Count - 1 then
    begin
      Delete(buf, Length(buf), 1);
      buf := buf +']);';
    end;
    sc.Add(buf);
    Inc(i);
  until i > ta.Fields.Count - 1;
  sc.SaveToFile('xxx.txt');
  sc.Free;
end;

The resulting code is shown below.  Simply create a new procedure in you data module and paste this source code in there and you are ready to clone data records.
InsertRecord atamea([
    nil,
    atamea.FieldByName('fname').AsString,
    atamea.FieldByName('mname').AsString,
    atamea.FieldByName('lname').AsString,
    atamea.FieldByName('addr').AsString,
    atamea.FieldByName('phone').AsString,
    atamea.FieldByName('apemail').AsString,
    atamea.FieldByName('appdate').AsString,
    atamea.FieldByName('ov18').AsString,
    atamea.FieldByName('wus').AsString,
    atamea.FieldByName('felony').AsString,
    atamea.FieldByName('felexp').AsString,
    atamea.FieldByName('posap').AsString,
    atamea.FieldByName('startdate').AsString,
    atamea.FieldByName('skillslictng').AsString,
    atamea.FieldByName('conpres').AsString,
    atamea.FieldByName('pwd').AsString,
    atamea.FieldByName('fmtname').AsString,
    atamea.FieldByName('cat').AsString,
    atamea.FieldByName('obj').AsString]);

Position the table to the record you would like to clone and call the newly created procedure and you record wil be cloned.

Notice the autoinc field is replaced with nil.  This is done so not to interfere with the table’s normal processing.

{*-----------------------------------------------------------------------------
  Procedure: Clone
  Author:    Mr. Arch Brooks, Software Engineer, Brooks Computing Systems LLC
  Date:      20-Aug-2015
  @Param     None
  @Return    None
-----------------------------------------------------------------------------}
 
procedure TBCSResumedm.Clone;
begin
    atamea.InsertRecord ([
    nil,
    atamea.FieldByName('fname').AsString,
    atamea.FieldByName('mname').AsString,
    atamea.FieldByName('lname').AsString,
    atamea.FieldByName('addr').AsString,
    atamea.FieldByName('phone').AsString,
    atamea.FieldByName('apemail').AsString,
    atamea.FieldByName('appdate').AsString,
    atamea.FieldByName('ov18').AsString,
    atamea.FieldByName('wus').AsString,
    atamea.FieldByName('felony').AsString,
    atamea.FieldByName('felexp').AsString,
    atamea.FieldByName('posap').AsString,
    atamea.FieldByName('startdate').AsString,
    atamea.FieldByName('skillslictng').AsString,
    atamea.FieldByName('conpres').AsString,
    atamea.FieldByName('pwd').AsString,
    atamea.FieldByName('fmtname').AsString,
    atamea.FieldByName('cat').AsString,
    atamea.FieldByName('obj').AsString]);
end;

Repeat this process for each table you desire records to be cloned from and you are all set.

This code can also be modified to generate the complete series of SQL statements for subsequent use.

The source code may be found by clicking here.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply