removing duplicates from combo box

dabwang
12-15-2003, 07:54 AM
is there any way of removing duplicate entries from a combo box. the enrties are in a database. a query could be implemented in access but is there any easy way of doing it in VB?

many thanks

nige

Ranma_at
12-15-2003, 08:04 AM
is there any way of removing duplicate entries from a combo box. the enrties are in a database. a query could be implemented in access but is there any easy way of doing it in VB?

many thanks

nige



Public Sub RemoveDoubles(OBox As ComboBox)
Dim i As Integer
Dim j As Integer
i = 0
Do
For j = (OBox.ListCount - 1) To (i + 1) Step -1
If OBox.List(j) = OBox.List(i) Then
OBox.RemoveItem j
End If
Next j
i = i + 1
Loop While i < OBox.ListCount
End Sub

gprinsloo
12-15-2003, 09:56 AM
The other way would be during inclusion.

Just for safety sake do a ucase to avoid case sensitive duplications.


dim dup
dup = false
For i = 0 To Combo1.ListCount - 1
If ucase(newitem) = ucase(Combo1.List(i)) Then dup = true
Next i
if dup = false then combo1.additem newitem

OnErr0r
12-15-2003, 10:03 AM
gprinsloo makes a good point, in that it's better to not add duplicates, rather than add them and then remove. You can check if an item exists more quickly using SendMessage and LB_FINDSTRINGEXACT.

If you have a combo/list that already contains dups for some reason, and it is sorted, you can simply do this to remove them:


Dim i As Long

For i = Combo1.ListCount - 1 to 1 Step -1
If Combo1.List(i) = Combo1.List(i - 1) Then ' add UCase$ or LCase$ if you care about case
Combo1.RemoveItem i
End If
Next i

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum