用一个ADOQuery查询出来不重复的列,然后逐记录地添加吧! 查询语句如下: SQL2000: select distinct 列名 from 表名 ACCESS: select distinctrow 列名 from 表名
关键字distinct 或distinctrow可保证无重重数据
一楼的方法很好, ADOQuery.First; while not ADOQuery.Eof do begin Combobox1.Items.add(FieldByName('列名').AsString); ADOQuery.Next end;
建立数据集后,用下面过程添加,或者用DBLookupComboBox连接数据集。procedure UpdateComboBox(MyComboBox: TCustomComboBox; MyADODataSet: TADODataSet; MyFieldName: String); begin MyComboBox.Clear; MyADODataSet.First; While not MyADODataSet.Eof do begin MyComboBox.Items.Add(MyADODataSet.FieldValues[MyFieldName]); MyADODataSet.Next; end; end;
或者可以用另一种方法: 用一个adoquery/adotable/query/table等控件之一连接到数据库,在其OnActive事件中编写代码如下: if combobox1.items.indexof(adoquery1.fieldbyname('字段名').asstring) then combobox1.items.add(adoquery1.fieldbyname('字段名').asstring); //方法是先判断该值在列表框中,如果不在,就添加进去
select 列名 from 表名 group by 列名
一楼的方法的实现: /////////////////////////////// cb_name是一个comboBox var B_find:boolean; I_temp:integer; begin with ADOQuery do begin close; sql.clear; sql.text:='select * from goodsmessage order by name'; open; while not eof do begin B_find:=false; for I_temp:=0 to cb_name.items.count-1 do begin if cb_name.Items[i_temp]=fieldbyname('name').asstring; then b_find:=true; if not b_find then cb_name.items.add(fieldbyname('name').asstring); next; end; close; end; end;
查询语句如下:
SQL2000:
select distinct 列名 from 表名
ACCESS:
select distinctrow 列名 from 表名
ADOQuery.First;
while not ADOQuery.Eof do begin
Combobox1.Items.add(FieldByName('列名').AsString);
ADOQuery.Next
end;
MyFieldName: String);
begin
MyComboBox.Clear;
MyADODataSet.First;
While not MyADODataSet.Eof do
begin
MyComboBox.Items.Add(MyADODataSet.FieldValues[MyFieldName]);
MyADODataSet.Next;
end;
end;
用一个adoquery/adotable/query/table等控件之一连接到数据库,在其OnActive事件中编写代码如下:
if combobox1.items.indexof(adoquery1.fieldbyname('字段名').asstring) then
combobox1.items.add(adoquery1.fieldbyname('字段名').asstring);
//方法是先判断该值在列表框中,如果不在,就添加进去
///////////////////////////////
cb_name是一个comboBox
var
B_find:boolean;
I_temp:integer;
begin
with ADOQuery do
begin
close;
sql.clear;
sql.text:='select * from goodsmessage order by name';
open;
while not eof do
begin
B_find:=false;
for I_temp:=0 to cb_name.items.count-1 do
begin
if cb_name.Items[i_temp]=fieldbyname('name').asstring;
then b_find:=true;
if not b_find then
cb_name.items.add(fieldbyname('name').asstring);
next;
end;
close;
end;
end;
为什么我用DBLookupComboBox控件后,下拉菜单里的东西不能选择啊?