简体   繁体   English

将记录从一个表复制到另一个表不起作用

[英]Copy record from one Table to another not working

I am using the BDE and flat Tables. 我正在使用BDE和平面表。 I have two identical Tables, tblOne and tblTwo I am trying to copy the data from one table to the other. 我有两个相同的表, tblOnetblTwo我正在尝试将数据从一个表复制到另一个表。 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. 逐步执行此操作,对于“ From”表,所有值均为“ Null”。

After the Post I get a blank record added to the tblTo . 发布后,我得到一个空白记录添加到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. 我添加了“ var”参数,以查看是否有什么不同,但没有。

Oh, by the by, I am starting the loop from "1" not "0" as the first field in both files is an AutoInc . 哦, AutoInc ,我正在从“ 1”而不是“ 0”开始循环,因为两个文件中的第一个字段都是AutoInc

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. 我不会用tblTo.Insert / tblTo.Post 里面 CopyRecord方法。 but rather use it outside eg: 而是在外部使用它,例如:

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

This could be re-used also in Edit mode. 也可以在“ Edit模式下重新使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Delphi:访问表的行或列,找到一种将数据从一个表复制到另一个表的方法 - Delphi : Access table rows or columns, Finding a way to copy data from one table to another 将XValues从一个TChartSeries复制到另一个 - Copy XValues from one TChartSeries to another Delphi内存拷贝与记录到另一个记录 - Delphi Memory copy with Record to another Record 如何将特定行从一个TRichEdit控件复制到另一个? - How to copy specific lines from one TRichEdit control to another? 如何将椭圆或多边形从一个位图复制到另一个位图 - How to copy ellipse or polygon from one bitmap to another bitmap 如何将子树从一个TTreeView复制到另一个? - How can I copy a subtree from one TTreeView to another? 在 Delphi 中处理从一种形式到另一种形式的复制组件 - Handle copy component from one form to another in Delphi 如何将数据从一个类复制到另一个类 - How to copy data from one class into another class 使用另一个表中的值对一个表中的字段进行oncalculate - do oncalculate fields in one table using values from another table 如何使用TSQLConnection / dbExpress将数据库的表复制到另一个数据库的相应数据库? - How to copy a database's table to another database's corresponding one using TSQLConnection/dbExpress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM