Page 1 of 1

Copy record to same table?

Posted: 11.04.2013, 21:48
by IbeDBob
Hi All,

Is there an easy way to copy a record o the same table?

I have an SQLite database that the user may often want to have almost the same stuff in another record, and just change one or two fields.

Is there something like BatchMove that I can copy all but the AutoInc field? There are no no unique, cannot be blank etc conflicts.

I know I can write a

...fieldbyname.AsString = ...fieldbyname..

for something like 120 fields but would rather just use something like CopyRecord(tblFrom, tblTo

Thanks

Posted: 12.04.2013, 15:15
by marsupilami
hmmm - something like that:

Code: Select all

procedure copyfields(Src, Target: TDataSet; Blacklist: String);
var
  x: Integer;
begin
  Blacklist := ';' + Blacklist + ';';
  for x := 0 to Src.Fields.Count do begin // iterate over all source fields
    if Pos(';' + Src.Fields[x].FieldName + ';', Blacklist) <> 0 then begin //check if the Field is in the blacklist
      Dst.FieldByName(Src.Fields[x].FieldName).Value := Src.Fields[x].Value;
    end;
  end;
end;
The Blacklist is a list of the fields you don't want to copy. The Fields are separated by Semicolons...

Posted: 15.04.2013, 20:58
by IbeDBob
Thanks for going to that trouble, but I had already tried something like that and it would not copy the data to the new record. It created and empty record. Here's what I tried...

Code: Select all

  for i:=1 to tblFrom.FieldCount-1 do  // Start from 1 to skip the AutoInc
  begin
    if tblTo.FindField(tblFrom.Fields[i].FieldName) <> nil then
    begin
      tblTo.FieldByName(tblFrom.Fields[i].FieldName).Value:=tblFrom.Fields[i].Value;
    end;
  end;
I finally got around it by creating a table at run time, Copying a record to it then back to the originating DataSet. That worked well.