为何老是要添加一条空记录????(50分)

P

pantera

Unregistered / Unconfirmed
GUEST, unregistred user!
请看如下代码:
Procedure TForm_Main.AddRecord;
//每月第一次使用时,自动根据上月的记录重建本月记录
begin
//检查如果数据库中上月记录为0,则不能自动添加记录,需手动添加记录
If Query1.RecordCount = 0 Then
begin
MessageDlg('数据库中没有上月的数据记录,' + #10#13 + '请手动添加记录!', mtInformation, [mbOK], 0);
Exit;
end;

With Query1 do
begin
First;
While not Eof do
begin
With QueryAMD DO
begin
Active := True;
//Append;
Insert;
Fields[0].Value := YearMonth + Copy(Query1.Fields[0].Value, 7, 4);
Fields[1].Value := Query1.Fields[1].Value;
Fields[2].Value := Query1.Fields[2].Value;
Fields[6].Value := Query1.Fields[6].Value;
Fields[3].Value := 0;
Fields[5].Value := 0;
Fields[4].Value := Date;
Fields[7].Value := '';
end;
Query1.Next;
end;

With QueryAMD do
begin
UpdateRecord;
ApplyUpdates;
Active := False;
end;

end;
end;
执行的结果,系统老是要添加一条空记录,这是为何?????请达人解答!!!!
 
While not Eof do

改成

While recno<>RecordCount do

当指针在最后一条记录时,eof仍然还是False,要再执行Next后才会变为True
因而多执行了一次Insert。
 
试试
With QueryAMD do
begin
post;
Active := False;
end;
 
TQuery 的Eof有时是有问题。可以这样试试:
With Query1 do
begin
First;
While (not Eof) and (not Bof) do...
 
While not Eof do
多做了一条记录
可用
for i:=1 to Query1.RecordCount do
 
With Query1 do
begin
First;
QueryAMD.Open; //在循环体外打开
While not Eof do
begin
With QueryAMD DO
begin
//Active := True; 不要每次循环都打开一次
//Append;
Insert;
Fields[0].Value := YearMonth + Copy(Query1.Fields[0].Value, 7, 4);
Fields[1].Value := Query1.Fields[1].Value;
Fields[2].Value := Query1.Fields[2].Value;
Fields[6].Value := Query1.Fields[6].Value;
Fields[3].Value := 0;
Fields[5].Value := 0;
Fields[4].Value := Date;
Fields[7].Value := '';
Post;
end;
Query1.Next;
end;
 
以上方法都试过,问题如故。。。。[:(!][:(!][:(!][:(!][:(!][:(!]
 
While not Eof do
begin
With QueryAMD DO
begin
Active := True;
Append;
//Insert;
Fields[0].Value := YearMonth + Copy(Query1.Fields[0].Value, 7, 4);
Fields[1].Value := Query1.Fields[1].Value;
Fields[2].Value := Query1.Fields[2].Value;
Fields[6].Value := Query1.Fields[6].Value;
Fields[3].Value := 0;
Fields[5].Value := 0;
Fields[4].Value := Date;
Fields[7].Value := '';
end;
// Query1.Next;
post;
end;
{With QueryAMD do
begin
UpdateRecord;
ApplyUpdates;
Active := False;
end;
}

 
用append不行嗎?你做得沒錯呀
 
你把with ..do全去掉,改到每个数据命令中去!!
 
[:(]楼上的老兄,还是不行啊!!![:(][:(][:(]
 
是不是原始数据中就有空行?
或者其他地方还有产生空行的代码?
 
回楼上,原始数据中没有空记录,并且只有这一段代码产生数据
 
顶部