procedure Thpxxglfm.cxbtClick(Sender: TObject);
var
i:integer;
str,str1,str2,str3:string;
begin
i:=1;
if (trim(cxtjbox.Text) ='')or (trim(sredit.Text )='') then //查询条件是combobox,其中有货品编码,货品名称,货品品类
begin
showmessage('请输入完整的查询信息!');
exit;
end;try
str:='select * from 货品信息表 where';
case cxtjbox.ItemIndex of
0:
   begin
   str:=str+'货品编码:=sr';
   str1:=str;
   i:=1;
   end;
1:
   begin
   str:=str+'货品名称:=sr';
   str2:=str;
   i:=2;
   end;
2:
   begin
   str:=str+'货品品类:=sr';
   str3:=str;
   i:=3;
   end;
end;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add(str);
parameters.ParamByName('sr').Value :=trim(sredit.Text );
open;
first;
if   recordcount=0 then
   begin
   showmessage('没有符合条件的查询!');
   sredit.SetFocus ;
   exit;
   end
   else
   begin
case i of
   1:  begin
   with adoquery2 do
       begin;
       close;
       sql.Clear;
       sql.Add('select * from 货品信息表 where 货品编码=hpbm');
       parameters.ParamByName('hpbm').Value :=trim(sredit.Text);
       open;
       with stringgrid1 do
       begin
       cells[0,1]:=fieldbyname('货品编码').AsString ;
       cells[1,1]:=fieldbyname('货品名称').AsString ;
       cells[2,1]:=fieldbyname('货品品类').AsString ;
       cells[3,1]:=fieldbyname('单位').AsString ;
       cells[4,1]:=fieldbyname('仓库编码').AsString ;
       cells[5,1]:=fieldbyname('仓库名称').AsString ;
       cells[6,1]:=fieldbyname('进货价').AsString ;
       cells[7,1]:=fieldbyname('原始库存').AsString ;
       cells[8,1]:=fieldbyname('警戒底线库存').AsString;
       cells[9,1]:=fieldbyname('警戒高线库存').AsString;
       end;
       end;
       end;
   2:  begin
   with adoquery2 do
       begin
       close;
       sql.Clear;
       sql.Add('select * from 货品信息表 where 货品名称='+''''+sredit.Text+'''');
       open;
       with stringgrid1 do
       begin
       cells[0,1]:=fieldbyname('货品编码').AsString ;
       cells[1,1]:=fieldbyname('货品名称').AsString ;
       cells[2,1]:=fieldbyname('货品品类').AsString ;
       cells[3,1]:=fieldbyname('单位').AsString ;
       cells[4,1]:=fieldbyname('仓库编码').AsString ;
       cells[5,1]:=fieldbyname('仓库名称').AsString ;
       cells[6,1]:=fieldbyname('进货价').AsString ;
       cells[7,1]:=fieldbyname('原始库存').AsString ;
       cells[8,1]:=fieldbyname('警戒底线库存').AsString ;
       cells[9,1]:=fieldbyname('警戒高线库存').AsString ;
       end;
       end;
       end;
    3: begin
    with adoquery2 do
       begin
       close;
       sql.Clear;
       sql.Add('select * from 货品信息表 where 货品品类='+''''+sredit.Text+'''');
       open;
       with stringgrid1 do
       begin
       cells[0,1]:=fieldbyname('货品编码').AsString ;
       cells[1,1]:=fieldbyname('货品名称').AsString ;
       cells[2,1]:=fieldbyname('货品品类').AsString ;
       cells[3,1]:=fieldbyname('单位').AsString ;
       cells[4,1]:=fieldbyname('仓库编码').AsString ;
       cells[5,1]:=fieldbyname('仓库名称').AsString ;
       cells[6,1]:=fieldbyname('进货价').AsString ;
       cells[7,1]:=fieldbyname('原始库存').AsString ;
       cells[8,1]:=fieldbyname('警戒底线库存').AsString ;
       cells[9,1]:=fieldbyname('警戒高线库存').AsString ;
       end;
       end;
       end;
end;
     end;
end;
查询结果用stringgrid显示,adoquery1,adoquery2都已连好

解决方案 »

  1.   

    sql.Add('select * from 货品信息表 where 货品编码=:hpbm');//这里少:
           parameters.ParamByName('hpbm').Value :=trim(sredit.Text);你最好把出错的现象写下来,别人容易判断些。
      

  2.   

    str:='select * from 货品信息表 where';
    case cxtjbox.ItemIndex of
    0:
       begin
       str:=str+'货品编码:=sr';
       str1:=str;
       i:=1;
       end;
    根据你上面的代码,你的sql语句应该是这样的
    select * from 货品信息表 where货品编码:=sr
    这有两个错误
    1、where 和“货品编码”之间没有空格
    2/、赋值应该这样写 货品编码=:sr  冒号应该在后边,sql中的赋值是"=",:代表后边的是参数