Simply tweak the code a little to keep track of the quantity used while iterating, eg:
var
LocNos, LotNos: string;
QtyWanted, QtyUsed: Double;
...
Table2.First;
while not Table2.Eof do
begin
QtyWanted := Table2QTY.AsFloat;
if QtyWanted > 0.0 then
begin
Query1.SQL.Text = 'SELECT * FROM Table1 WHERE ITEMCODE = :ItemCode AND LOCQTY > 0';
Query1.ParamByName('ItemCode').AsString := Table2ItemNo.AsString;
Query1.Open;
try
Query1.First;
if not Query1.Eof then
begin
LocNos := '';
LotNos := '';
repeat
QtyUsed := Math.Min(Query1.FieldByName('LOCQTY').AsFloat, QtyWanted);
LocNos := LocNos + Query1.FieldByName('LOCNO').AsString + '=' + FloatToStr(QtyUsed) + ',';
LotNos := LotNos + Query1.FieldByName('LOTNO').AsString + ',';
QtyWanted := QtyWanted - QtyUsed;
Query1.Next;
until Query1.Eof or (QtyWanted <= 0.0);
SetLength(LocNos, Length(LocNos)-1);
SetLength(LotNos, Length(LotNos)-1);
Table2.Edit;
Table2LOCNO.AsString := LocNos;
Table2LOTNO.AsString := LotNos;
Table2.Post;
end;
finally
Query1.Close;
end;
end;
Table2.Next;
end;