Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > removing duplicates from combo box


Reply
 
Thread Tools Display Modes
  #1  
Old 12-15-2003, 07:54 AM
dabwang dabwang is offline
Regular
 
Join Date: Nov 2003
Posts: 79
Default removing duplicates from combo box


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
Reply With Quote
  #2  
Old 12-15-2003, 08:04 AM
Ranma_at's Avatar
Ranma_at Ranma_at is offline
Junior Contributor
 
Join Date: Oct 2003
Location: @WinMain
Posts: 211
Default

Quote:
Originally Posted by dabwang
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




Code:
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
Reply With Quote
  #3  
Old 12-15-2003, 09:56 AM
gprinsloo gprinsloo is offline
Centurion
 
Join Date: Nov 2003
Location: Paraguay
Posts: 138
Default

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
Reply With Quote
  #4  
Old 12-15-2003, 10:03 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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:

Code:
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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing Duplicates? Dphanman Word, PowerPoint, Outlook, and Other Office Products 4 08-06-2003 07:38 AM
Removing item from a combo box Schroeder Interface and Graphics 1 07-18-2003 10:42 AM
Help with 'Removing Duplicates' Code snyderje Excel 2 07-11-2003 09:13 AM
Removing Duplicates... ozziefi General 1 06-18-2003 03:53 PM
Remove Duplicates from Combo Box Shaitan00 Database and Reporting 1 06-04-2003 12:02 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->