循环语句?

hxh81 2001-06-28 09:43:26
有两表,table1:货品编号,货品名称,计量单位,库存量,剩余量
table2:货品编号,货品名称,计量单位,领料量
当table2有几条记录,按某一按钮,需要做的事为:
从table2第一条记录开始检测,确定table1中是否有你要领的货品,如果有,还要看table1中是否有足够的数量,如果有,剩余量就等于库存量—领料量,如果没有足够的数量,就提醒用户。如果table1中没有你要领的货品,也提醒用户。然后,再检测第二条记录,直到table2最后一条记录,怎样写循环语句,因为我写的语句出现死循环,此问题特急,请各位一定帮忙!
...全文
81 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxh81 2001-06-30
  • 打赏
  • 举报
回复
为什么是没人回答我的问题呢?
hxh81 2001-06-30
  • 打赏
  • 举报
回复
to passos:我用你回答的试了一下,但是有很多的问题:
我把代码改了:
首先写一个查询函数:
function find(const otable:ttable;const ofield:tfield;const svalue:string):boolean;

function find(const otable:ttable;const ofield:tfield;const svalue:string):boolean;
var
bmpos:tbookmark;
bfound:boolean;
begin
result:=false;
bfound:=false;
if not otable.Active then exit;
if otable.FieldDefs.IndexOf(ofield.fieldname)<0 then
exit;
bmpos:=otable.getbookmark;
with otable do
begin
disablecontrols;
first;
while not eof do
if ofield.asstring=svalue then
begin
result:=true;
bfound:=true;
break;
end
else
next;
end;
if (not bfound) then
otable.GotoBookmark(bmpos);
otable.FreeBookmark(bmpos);
otable.Enablecontrols;
end;


var
i,j:integer;
result:boolean;
begin
for i := 0 to datamodule1.hp1.RecordCount - 1 do
begin
result:=find(datamodule1.hp1,datamodule1.hp1.FieldByName('货品编号'),datamodule1.nl1bdedesigner3.AsString);
if not result then begin
showmessage('没有库存');
continue;
datamodule1.nl1.Next;
end;
for j := 0 to datamodule1.nl1.RecordCount - 1 do
begin
if datamodule1.nl1.FieldByName('数量').Asfloat > datamodule1.hp1.FieldByName('总库存').Asfloat then
begin
ShowMessage('库存不够。');
Continue;
datamodule1.nl1.Next;
end;
try

datamodule1.nldh1.post;
datamodule1.nl1.post;
datamodule1.nlquery1.Close;
datamodule1.nlquery1.SQL.clear;
datamodule1.nlquery1.SQL.Add('SELECT 货品编号, SUM( 数量 ) 领料量 FROM "领料单1.db" 领料单1 GROUP BY 货品编号');
datamodule1.nlquery1.Open;
datamodule1.hp1.post;
except
datamodule1.nldh1.cancel;
datamodule1.nl1.cancel;
datamodule1.hp1.Cancel;
ShowMessage('数据库处理出错。');
Exit;
end;
end;
datamodule1.hp1.Next;
end;
end;
如果我第一条记录在table1中不存在,或库存不够,会提示,但是会出现很多次提示,
且库存不够,它也会领,库存为负数,现在我想库存不够时,不给领用。
但是他并不会检测第二条记录了,怎么办?

TangDL 2001-06-29
  • 打赏
  • 举报
回复
好象Table1、Table2设反了,不好意思
要不你用Query自己写SQL语句
说白了,Table还是用的SQL语句
TangDL 2001-06-29
  • 打赏
  • 举报
回复
设两个Table:Table1,Table2
两个Table指向同一个数据库
Table1设为表1,Table2设为表2
Table2的MasterSource设为指向Table1的DataSource
Table2的MasterFields属性设为货品编号或货品名称
(有对话框向导设置的)
此后,Table1.next时,Table2的记录会自动选取与Table1相关的值
在Table2中作统计和所说的操作应是很简单的了
石榴刺猬 2001-06-29
  • 打赏
  • 举报
回复
给你代码,其中字段名换成你自己的。

Table1.First;
for i := 0 to table2.RecordCount - 1 do
begin
Table2.Filter := '货品编号 = ' + Table1.FiledByName('货品编号').AsString;
Table2.Filtered := True;

if Table2.RecordCount = 0 then
begin
ShowMessage('没有库存');
Continue;
end;

for j := 0 to Table1.RecordCount - 1 do
begin
if Table2.FieldByName('领料量').AsInteger < Table1.FiledByName('库存量').AsInteger then
begin
ShowMessage('库存不够。');
Continue;
end;

Table1.Edit;
Table1.FiledByName('剩余量').AsInteger := Table1.FieldByName('库存量').AsInteger - Table2.FieldByName('领料量');
try
Table1.Post;
except
Table1.Cancel;
ShowMessage('数据库处理出错。');
Exit;
end;

Table1.Next;
end;

Table2.Next;
end;
qhuojun 2001-06-29
  • 打赏
  • 举报
回复
这种情况你应该跟踪调试,看看到底哪条语句导致死机.
你不妨试试.
hxh81 2001-06-29
  • 打赏
  • 举报
回复
以上的答案我都试过了,不行,这个问题很急,能不能帮帮忙!
xclxd 2001-06-28
  • 打赏
  • 举报
回复
掉了begin,

while not table2.eof() do
begin
...
table2.next;

end;

xclxd 2001-06-28
  • 打赏
  • 举报
回复
while not table2.eof() do
...
table2.next;

end;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧