各位大侠好,小弟在这里请教帮忙改正一个问题,对者高分相送.
现在有两个表,一个为员工部门表,用来存放部门编号和部门名称;另一个员工表用来存放员工基本信息,其中部门编号字段和员工部门表对应项对应.我想建立一个combobox当点击按钮1的时候部门名称存到combobox的下拉列表框中,当我选择combobox中一项时对应部门名称的员工信息显示在DBGrid表中。但是我的程序不能显示对应员工的信息,请教哪位大侠给予指教一下,一定给分的!
procedure TForm1.Button1Click(Sender: TObject);
begin
with query1 do
begin
close;
SQL.Clear;
SQL.Add('select * from bumen');
open;
first;
combobox1.Items.Add('所有部门');
while not eof do
begin
combobox1.Items.Add(fieldByName('bmmc').AsString);
next;
end;
combobox1.ItemIndex:=0;
end;
end;
//以上正确,错误在下面!
procedure TForm1.ComboBox1Click(Sender: TObject);
var
st,str:string;
begin
 if combobox1.ItemIndex=0 then
 str:='select * from t_ygb'
 else
 begin
 st:=intTostr(combobox1.itemIndex);
 str:='select * from t_ygb where bmbh=:st';
 end;
 query2.Close;
 query2.SQL.Clear;
 query2.SQL.Add(str);
 query2.Open;
end;end.

解决方案 »

  1.   

    你确认这个关系是对应的吗 bmbh=:ststr:='select * from t_ygb where bmbh=:st'; 有错误应为 str:='select * from t_ygb where bmbh='+'"'+st+'"';
      

  2.   

    应为 str:='select * from t_ygb where bmbh='+'"'+st+'"';
    或var
    st,str:string;
    begin
     if combobox1.ItemIndex=0 then
     str:='select * from t_ygb'
     else
     begin
     str:='select * from t_ygb where bmbh=:st';
     end;
     query2.Close;
     query2.SQL.Clear;
    query2.SQL.Add(str);
    query2.parambyname('st').asstring:=intTostr(combobox1.itemIndex);
     query2.Open;
      

  3.   

    combobox1.itemIndex得到的是combobox1的选择项的索引,也就是你选择的是第几项,但并不是选择项目的内容,如果第一项的内容为'1001',combobox1.itemIndex为1,combobox1.text才是‘1001’,所以应该改成
    st:=combobox1.Text
      

  4.   

    在用字符型变量进行SQL语句连接时,必须加上引号;在楼主的
    str:='select * from t_ygb where bmbh=:st'中,:str是用于参数查询时的格式,应该改成
    str:=select * from t_ygb where bmbh=''' + st + '''';
      

  5.   

    同意楼上的
    str:=select * from t_ygb where bmbh=''' + st + '''';
    或者query2.Close;
     query2.SQL.Clear;
     query2.SQL.Add(str);
    query2.params[0].asstring:=st;
     query2.Open;
    str:='select * from t_ygb where bmbh=:st';
    是参数形式的sql语句
    所以要在query2.open之前取得参数st的值
      

  6.   

    st:=intTostr(combobox1.itemIndex);
     str:='select * from t_ygb where bmbh=:st';
     end;
    SQL语句错了应写成:
    st:=intTostr(combobox1.itemIndex);
    query2.Close;
    query2.sql.text:='select * from t_ygb where bmbh=:dd'
    query2.Params.ParamValues['dd']:=st;
    query2.open
      

  7.   

    procedure TForm1.ComboBox1Click(Sender: TObject);
    var
    st,str:string;
    begin
     if combobox1.text='' then
     str:='select * from t_ygb'
     else
     begin
     st:=combobox1.text;
     str:='select * from t_ygb,bumen where bumen.编号=t_ygb.部门编号 and bumen.编号=:st';
     end;
     query2.Close;
     query2.SQL.Clear;
     query2.SQL.Add(str);
     query2.Open;
    end;
    这样就好了吧~前面你那个SQL些的有问题啊。你combobox1里存的是部门名称。员工表存的是编号,用名称在员工表里肯定找不到啊。
      

  8.   

    如还出错你在查询的时候也可以这么些
    query2.sql.add('select * from t_ygb,bumen where bumen.编号=t_ygb.部门编号 ');
    query2.sql.add('and bumen.编号='+#39+st#39);
    #39表示单引号
      

  9.   

    不好意思写错了。呵呵。。
    query2.sql.add('and bumen.编号='+#39+st+#39);
    放在str里就是'select * from t_ygb,bumen where bumen.编号=t_ygb.部门编号 and bumen.编号='+#39+st+#39;