各位大侠好,小弟在这里请教帮忙改正一个问题,对者高分相送.
现在有两个表,一个为员工部门表,用来存放部门编号和部门名称;另一个员工表用来存放员工基本信息,其中部门编号字段和员工部门表对应项对应.我想建立一个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.
现在有两个表,一个为员工部门表,用来存放部门编号和部门名称;另一个员工表用来存放员工基本信息,其中部门编号字段和员工部门表对应项对应.我想建立一个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.
或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;
st:=combobox1.Text
str:='select * from t_ygb where bmbh=:st'中,:str是用于参数查询时的格式,应该改成
str:=select * from t_ygb where bmbh=''' + st + '''';
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的值
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
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里存的是部门名称。员工表存的是编号,用名称在员工表里肯定找不到啊。
query2.sql.add('select * from t_ygb,bumen where bumen.编号=t_ygb.部门编号 ');
query2.sql.add('and bumen.编号='+#39+st#39);
#39表示单引号
query2.sql.add('and bumen.编号='+#39+st+#39);
放在str里就是'select * from t_ygb,bumen where bumen.编号=t_ygb.部门编号 and bumen.编号='+#39+st+#39;