Copy records from a table to another table  

Send By: Jesus Cardenas
Web : N.D.
Email: jesusc@tfs.grupo-ed.com
Date: 17/08/00

Tip accessed 83 times

 


Jesus Cardenas has sent us these two procedures to copy a record from a table to another.
The advantage that they offer is that the structures of the tables don't have to be same.


This procedure adds records from a source table to a destination table, making comparisons for the name of the fields. The destination table remain in edit state. This routine only works for the fields associated with the object TField. These you can include them using the object inspector.


 procedure AppendFrom;
 var
   i : integer;
   fldDest, fldSource : TField;
 begin
   with tblDest do begin
     Append;
     for i := 0 to FieldCount - 1 do
     begin
       fldDest := Fields[i];
       if not (fldDest.ReadOnly or fldDest.Calculated) then
       begin
         fldSource := tblSource.FindField(fldDest.FieldName);
         if assigned(fldSource) then
         begin
           fldDest.DataSet.Edit;
           fldDest.AsString := fldSource.AsString
         end
       end
     end;
   {Post}
   end
 end;





This other procedure, makes the same thing that the previous one, but instead of managing the fields TField, it uses all the fields of the table. The fields are compared by name between the table origin and the destination.


 procedure AppendFromAllFields;
 var
   i : integer;
   fldDest, fldSource : TField;
   tblSource, tblDest : TTable;
 begin
   tblSource := nil;
   tblDest := nil;
   try
     tblSource := TTable.Create(nil);
     tblDest := TTable.Create(nil);
     tblSource.DatabaseName := tblS.DatabaseName;
     tblSource.TableName := tblS.TableName;
     tblDest.DatabaseName := tblD.DatabaseName;
     tblDest.TableName := tblD.TableName;
     tblDest.Open;
     tblSource.Open;
     with tblDest do
     begin
       Append;
       for i := 0 to FieldCount - 1 do
       begin
         fldDest := Fields[i];
         if fldDest.AsString = '' then
         begin
           fldSource := tblSource.FindField(fldDest.FieldName);
           if assigned(fldSource) then
             fldDest.AsString := fldSource.AsString;
         end;
       end;
       Post;
     end;
   finally
     tblDest.Free;
     tblSource.Free;
   end;
 end;





Improvement sent by: C. Aguado (cesagu@teleline.es)



  • 1. - if we have in an index a field of kind Time, we can have an error of repeated Index when using the method AsString, since this it rounds the hour to the format hh:mm:ss, without keeping in mind the miliseconds. I have him avoiding using it 'Value' instead of 'AsString'.
  • 2. - when we copy the data of a field of a table that it is not marked as " Required " to another in that if he is, we obtain an exception if the field of the first chart is empty, for what it is necessary to check it and in such a case to introduce something in the one. I introduce a 0, what gives me good results for my use, but I don't know if it will always be correct.

    The listing with the changes that I have made in the original code to correct both first you hit it is the following one:


                 if Assigned(fldSource) then
                   begin
                     fldDest.Dataset.Edit;
                     if fldSource.Isnull then
                     begin
                       if fldDest.Required and fldSource.Isnull then
                           fldDest.Value:= 0; {ojo con esto
                           ya que si hay campos vacios en una tabla y cambiamos esos
                           campos a requeridos, puede haber problemas al convertirlos.}
                     end else fldDest.Value:= fldSource.Value;
                   end;
                 end;
    




    Updated at 17/08/2000