我库存表(kcb)中记录的是出入库的过程记录,即入库时,库存表(kcb)中的库存数量为正;出库时,库存数量为负。由于在出库时,没有和sum(库存数量)相比较,于是出现出库数量大于库存数量时,还能出库,导致库存数量为负,显然不符合逻辑。于是我加了一段代码,当出库单中输入的数量小于sum(库存数量)时,提示‘库存数量不足,请输入合适的出库量!’。但是可能加代码位置不对,导致我不管输入多少出库数量,总是提示‘库存数量不足,请输入合适的出库量!’。请大家指点一下,到底什么地方错了!谢谢各位了!
添加的代码如下:
const t = ''''; //t=单引号
var
   nowStore:real;
   s:string;
.................
.................
             adoq3.Connection :=adoc;
             adoq3.SQL.Clear ;
             s := '';
             s := 'select sum(库存数量) as cout from kcb where 商品编码 =' ;
             s := s + (t + edit7.Text +t) +' group by 商品编码';
             adoq3.SQL.add(s);
             adoq3.open;
             nowStore:=adoq3.FieldByName('cout').AsInteger;
             if (strtoint(edit14.Text ))> nowStore then
                showmessage('当前库存数量已不足,请修改数量')
总的代码如下:
procedure Txsh_form.SpeedButton15Click(Sender: TObject);
 const t = ''''; //t=单引号
var
   nowStore:real;
   s:string;
 //  content:real;
begin
if (length(edit2.Text)<>0) then
    begin
       with database do
       begin
       if (length(edit13.Text)=0) and (length(edit14.Text)=0) then
             begin
             edit13.Text :='0';edit14.Text :='0';
             end;
             adoq3.Connection :=adoc;
             adoq3.SQL.Clear ;
             s := '';
             s := 'select sum(库存数量) as cout from kcb where 商品编码 =' ;
             s := s + (t + edit7.Text +t) +' group by 商品编码';
             adoq3.SQL.add(s);
             adoq3.open;
             nowStore:=adoq3.FieldByName('cout').AsInteger;
             {
             s := '';
             s := 'select 库存数量 from kcb where 商品编码 =' ;
             s := s + (t + edit7.Text +t);
             adoq3.SQL.add(s);
             adoq3.open;
             nowStore:=adoq3.FieldByName('库存数量').AsInteger;
             }
             //showmessage(s + '\n' + adoq3.FieldByName('库存数量').AsString);
             if (strtoint(edit14.Text ))> nowStore then
                showmessage('当前库存数量已不足,请修改数量')
             else
                begin
                edit29.Text :=floattostr(strtofloat(edit13.Text)*strtofloat(edit14.Text));
                edit30.Text :=formatdatetime('yy-mm-dd',datetimepicker1.date);
                edit33.Text :=edit2.Text ;
                edit34.Text :=formatdatetime('MM-dd-yy',datetimepicker1.date);
                adoq1.Connection :=adoc;
                adoq2.Connection :=adoc;
                adoq1.SQL.Clear ;
                adoq1.SQL.Add('insert into xshd(录单日期,单据编码,供货商名称,经手人,备注,商品简码,商品编码,商品名称,类别,规格,型号,单位,参考单价,数量,金额) values(:ldrq,:djbm,:gysmc,:jsr,:bz,:spjm,:spbm,:spmc,:lb,:gg,:xh,:dw,:ckdj,:sl,:je)');
                adoq1.parameters.ParamByName('ldrq').value :=edit30.Text ;
                adoq1.parameters.ParamByName('djbm').value :=edit2.text;
                adoq1.parameters.ParamByName('gysmc').value :=edit3.text;
                adoq1.Parameters.ParamByName('jsr').Value :=edit4.Text;
                adoq1.Parameters.ParamByName('bz').Value :=edit5.text;
                adoq1.parameters.ParamByName('spjm').value :=edit6.text;
                adoq1.parameters.ParamByName('spbm').value :=edit7.text;
                adoq1.parameters.ParamByName('spmc').value :=edit8.text;
                adoq1.Parameters.ParamByName('lb').Value :=edit9.Text;
                adoq1.Parameters.ParamByName('gg').Value :=edit10.text;
                adoq1.parameters.ParamByName('xh').value :=edit11.text;
                adoq1.parameters.ParamByName('dw').value :=edit12.text;
                adoq1.parameters.ParamByName('ckdj').value :=edit13.text;
                adoq1.Parameters.ParamByName('sl').Value :=edit14.Text;
                adoq1.Parameters.ParamByName('je').Value :=edit29.Text;
                adoq1.Active :=true;
                adoq1.ExecSQL ;
                adoq2.SQL.Clear ;
                adoq2.SQL.Add('select 编号,单据编码,供货商名称,商品简码,商品编码,商品名称,类别,规格,型号,单位,参考单价,数量 from xshd where 单据编码=:djbm and 录单日期 between #'+edit34.text+ '# and #' +edit34.text+'#');
                adoq2.Parameters.ParamByName('djbm').Value :=edit33.Text;
                adoq2.Active :=true;
                datas1.DataSet :=adoq2;
                dbgrid1.DataSource:=datas1;
                statusbar1.Panels[1].Text :=inttostr(adoq2.Recordset.RecordCount);
                edit6.Clear ;
                edit7.Clear ;edit8.Clear ;edit9.Clear ;edit10.clear;edit11.Clear;edit12.clear;
                edit13.Text :='0';
                edit14.Text :='0';
                datetimepicker1.date:=date;
                if (length(edit2.Text)<>0) and (length(edit3.Text)<>0) then
                edit6.SetFocus ;
                end;
         end;
//end ;
         end
      else
         begin
         showmessage('请输入销售单号');
         if (length(edit2.Text)<>0) and (length(edit3.Text)<>0) then
         edit6.SetFocus
         else edit2.SetFocus ;
         end;end;

解决方案 »

  1.   

    nowStore:=adoq3.FieldByName('cout').AsInteger; 
    这句话不是已经赋值给nowStore了吗?
      

  2.   

    看前面的代码没看到什么问题??
    1就是nowStore的类型,和从数据库中获取的类型不一致,需要改一下。
    2adoquery需要先关闭。
    with adoquery do
    begin
      close;
      sql.clear;
      sql.add(sqlstr);
      open;
    end;
    3我个人意见,数据库字段最好用英文。
      

  3.   

    LZ以前用Java还是用C的?
    Delphi 里面的大括号{}好像是块注释码吧!LZ代码全文中居然用了{},那不是把这段代码给注释掉了???
      

  4.   

    不好意思看错了一块代码。忽略我上面的答复吧。LZ取到的nowStore有内容的吗?