1

I am using the BDE and flat Tables. I have two identical Tables, tblOne and tblTwo I am trying to copy the data from one table to the other. Not the entire DB, just one specific Record using this:

function Tdm.CopyRecord(var tblFrom,tblTo : TTable) : Boolean;
var
  i : Integer;
begin
  Result:=False;
  try
    tblTo.Insert;
    for i:=1 to tblFrom.FieldCount-1 do
    begin
      if tblFrom.Fields[i].FieldName = tblTo.Fields[i].FieldName then
        tblTo.Fields[i].Value:=tblFrom.Fields[i].Value;
    end;
    tblTo.Post;
    Result:=True;
  finally
  end;
end;

if CopyRecord(tblOne,tblTwo) then...

Stepping through this all of the Values are "Null" for the From Table.

After the Post I get a blank record added to the tblTo. Not surprising with all the Values a Null. :)

Where am I going wrong in copying the data? It is not making it to the Copy function.

I have been at this for several hours and cannot make it work. Probably something simple I am over-looking. I added the "var" parameter to see if that made any difference but it did not.

Oh, by the by, I am starting the loop from "1" not "0" as the first field in both files is an AutoInc.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • 1
    Is there any reason you aren't using TDataSet.CopyFields? (such as using an old [pre D2006] version of Delphi) http://docwiki.embarcadero.com/Libraries/XE3/en/Data.DB.TDataSet.CopyFields – Gerry Coll Mar 17 '13 at 21:13
  • 2
    `if tblTo.FindField(tblFrom.Fields[i].FieldName) <> nil then tblTo.FieldByName(tblFrom.Fields[i].FieldName).Value := tblFrom.Fields[i].Value;` - it will be more accurate. – Abelisto Mar 17 '13 at 21:23
  • 2
    Are you sure there's nothing wrong with your tblFrom? Make sure it's open and that there is actually at least one record in it to copy. – Rob McDonell Mar 18 '13 at 01:55

1 Answers1

5

Here is how I would do it:

function CopyRecord(tblFrom, tblTo: TTable; const StartIndex: Integer=0): Boolean;
var
  i: Integer;
  FieldFrom, FieldTo: TField;
begin
  Result := False;
  for i := StartIndex to tblFrom.FieldCount - 1 do
  begin
    FieldFrom := tblFrom.Fields[i];
    FieldTo := tblTo.FindField(FieldFrom.FieldName);
    if Assigned(FieldTo) then
    begin
      FieldTo.Value := FieldFrom.Value;
      Result := True;
    end;
  end;
end;

I would not use tblTo.Insert/tblTo.Post inside the CopyRecord method. but rather use it outside e.g:

tblTwo.Append;
if CopyRecord(tblOne, tblTwo, 1) then
  tblTwo.Post
else
  tblTwo.Cancel;

This could be re-used also in Edit mode.

kobik
  • 21,001
  • 4
  • 61
  • 121
  • Thanks for the suggestion, I am using D5, sorry, should have mentioned it. –  Mar 17 '13 at 23:01
  • Thanks for the suggestion, I am using D5, sorry, should have mentioned it. Still the same result with Kobik's code. Blank line in the Target file. Stepping through, none of them passed into the "if assigned(FieldTo) then" I set a break point inside that and it never got there. It shows the correct number of Fields (15) to iterate through but all "From" values are still Null. Stupid thing here didn't allow me to finish editing. Five minutes is too short when it is all too easy to hit Enter and terminate the comment -- a zillion times. –  Mar 17 '13 at 23:08
  • 1
    minor mistake found by @snowfrog, mentioned in a NAA: `It has to be: FieldTo := tblTo.FindField(FieldFrom.FieldName);` – bummi Dec 09 '14 at 13:28