怎樣用一個查詢結果添加到listbox中?(100分)

K

kingson

Unregistered / Unconfirmed
GUEST, unregistred user!
我想實現這樣的結果:有一個combobox控件,一個listbox控件,當選擇combobox中的內容時,
listbox得到combobox中相關的內容:我是這樣做的
with query do
begin
close;
sql.clear;
sql.add('select * from tablename where field='+''''+combobox.text+'''');
open;
end;
listbox.items.clear;
query.first;
while not query.eof do
begin
listbox.items.add(query.fieldbyname('field').asstring;
next;
end;
可選擇combobox中的內容時,listbox中沒有內容,並且佔用資源,是什麼原因?請教!
 
combobox如果只是选择的话把它的Style设成DropDownList,这时combobox的Text属性已不好
用,你用comboBox1.Items.Strings[comboBox1.ItemIndex]替代你的comboBox.Text即可。
 
你跟踪一下看看
 
to YFeral:就算设成DropDownList也是好用的
to Kingson:你是在combobox的onchange事件中写的这些么?还有你倒数第二行的next好像
没指定是谁next吧,赫赫
 
with query do
begin
close;
sql.clear;
sql.add('select * from tablename where field='''+combobox.text+''''); <---
open;
end;
listbox.items.clear;
query.first;
while not query.eof do
begin
listbox.items.add(query.fieldbyname('field').asstring;
query.next;
end;
 
在combobox中的onchange的事件中执行此段程序。
另:跟踪执行一下,
listbox.items.add(query.fieldbyname('field').asstring
看在listbox中有没有添加进数据来
 
combobox如果只是选择的话把它的Style设成DropDownList
用comboBox1.Items.Strings[comboBox1.ItemIndex]替代你的comboBox.Text即可。
同意楼上的
 
to naughtboy:
我用了comboBox不知多少次了,当Style 为csDropDownlist时是不能通过Text属性改变
和获得ComboBox的文本的。不信你试试别瞎说!
不过楼主的最后的Next 没指定对象确实是错误的我没看清楚当时。
to All:
别人说过的不要重复好不好。
 
Rs_dm是數據模塊;v_bm_emp是一個query,字段有yjbm_name,ejbm_name,name,empid,yjbm_id,ejbm_id
但是我把combobox1.text改為combobox1.Items.Strings[combobox1.itemindex還是不行,listbox1中顯示不出ejbm_name,並且佔用資源,不動了!
procedure TF_overday.ComboBox1Change(Sender: TObject);
begin
rs_dm.v_bm_emp.Open;
with rs_dm.v_bm_emp do
begin
close;
sql.Clear;
sql.add('select ejbm_name from v_bm_emp where yjbm_name='+''''+combobox1.Items.Strings[combobox1.itemindex]+'''');
open;
end;
listbox1.Items.Clear;
rs_dm.v_bm_emp.First;
while not rs_dm.v_bm_emp.Eof do
begin
listbox1.Items.add(rs_dm.v_bm_emp.fieldbyname('ejbm_name').AsString);
next;
end;

end;
還是不行?
 
你的代码没什么错(除了几个小错以外)。
while not query.eof do
begin
listbox.items.add(query.fieldbyname('field').asstring);
query.next;
end;
你可以跟踪一下看query是否查询到结果,也许是根本就没有结果。
open;
if query.eof then
showmessage('没有查询到匹配的数据');

 
with query1 do
for I:=0 to RecordCount-1 do
begin
s:=query1['字段名'];
listBox1.Items.Add(s);
Table1.Next;
end;
 
with query do
begin
close;
sql.clear;
sql.add('select * from tablename where field='''+Trim(combobox.text)+''''); ---参数设置错误
open;
showmessage(inttostr(recordcount)); ----此处最好跟踪是否的到数据
first;
if not eof then
begin
listbox.items.clear
for i:=0 to recordcount-1 do
begin
listbox.items.add(fieldbyname('field').asstring;
next;
end;
end;
end;
 
還是不行,有沒有能提出個例子來看看!
 
我认为原因是:
while not query.eof do
begin
listbox.items.add(query.fieldbyname('field').asstring;
next[red];///要把query加上,即:query.next,不加的话,系统要一直找属于此属性的对象[/red]
end;
 
不会吧,问题挺菜的。应该早解决了。
 
顶部