向vb的combo中添加数据,怎样过滤掉重复数据??(100分)

F

foxmail

Unregistered / Unconfirmed
GUEST, unregistred user!
在vb中我想把表fox中的“品种”字段里的内容全部添加到combo控件的list中,
可写为
Set d1 = OpenDatabase("c:/wy.mdb")
Set r1 = d1.OpenRecordset("select * from fox")
r1.MoveFirst
Do Until r1.EOF
Combo1.AddItem r1!品种
r1.MoveNext
Loop
r1.Close
d1.Close
但现在“品种”字段中有很多重复数据,如何将重复数据不列入combo控件中?
请大家说说看有什么简单易用的方法??!!
 
select distinct 品种 from fox
 
Set r1 = d1.OpenRecordset("select * from fox")
------->
Set r1 = d1.OpenRecordset("select DISTINCT * from fox")
 
这样我认为不好!有没有别的方法??
 
靠,自己写个循环,没加入一个品名倒列表里就判断跟前面的有没有重复的,行了吧
 
问题是这个循环我调了半天老死机才问大家的呀!
 
是不是死循环
distinct是最好的了!
 
我现在就需要那个循环,下次遇到类似的问题就好解决了!
 
你一定要用循环的话,最好是按照品种进行排序:
Set r1 = d1.OpenRecordset("select * from fox order by 品种")
其实使用vb的DataCombo控件不是很爽吗,干吗要用Combo控件
 
select DISTINCT * from fox这句语句好象有问题!我只想把一个字段取出来。
应该写成select 品种 DISTINCT * from fox吗?
另外有没有人把这个循环语句写给我?
 
使用Zane的方法就可以呀,
Set d1 = OpenDatabase("c:/wy.mdb")
Set r1 = d1.OpenRecordset("select distinct 品种 from fox")
如果使用循环的话:
if r1.recordcount>0 then
'首先要判断是否有品种,否则r1.MoveFirst将出错
r1.MoveFirst
do
Until r1.EOF
Combo1.AddItem r1!品种
r1.MoveNext
Loop
end if
r1.Close
d1.Close
2. 如果使用DataCombo数据帮定控件的话,根本不需要循环,DataCombo控件于Combo的显示界面是一模一样Set d1 = OpenDatabase("c:/wy.mdb")
Set r1 = d1.OpenRecordset("select distinct 品种 from fox")
set DataCombo1.DataSource=r1
DataCombo1.ListField="品种"
r1.Close
d1.Close
 
这是个很简单的VB小技巧,会VB的都应该会啊!
利用错误处理和集合对象的key值,可以这样:
Dim i As Integer
Dim s As String
Dim c As New Collection
On Error Resume Next
Set d1 = OpenDatabase("c:/wy.mdb")
Set r1 = d1.OpenRecordset("select * from fox")
r1.MoveFirst
Do Until r1.EOF
s = Trim$(r1!品种 &
"")
c.Add s, s
r1.MoveNext
Loop
r1.Close
d1.Close
For i = 1 To c.Count
Combo1.AddItem c(i)
Next

Dim s As String
Dim c As New Collection
On Error Resume Next
Set d1 = OpenDatabase("c:/wy.mdb")
Set r1 = d1.OpenRecordset("select * from fox")
r1.MoveFirst
Do Until r1.EOF
s = Trim$(r1!品种 &
"")
c.Add s, s
If Err.Number <> 0 then
Err.Clear
else
Combo1.AddItem s
r1.MoveNext
Loop
r1.Close
d1.Close
 
用循环吧?楼上的可以看看。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
822
SUNSTONE的Delphi笔记
S
I
回复
0
查看
609
import
I
顶部