Results 1 to 15 of 15

Thread: Remove Duplicates From ComboBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Remove Duplicates From ComboBox

    Ok my combo boxes get filled using a database on formload.

    Is there a way to see if something is already in the combo box therefore not adding it again ? (Assuming this would be slow?)

    or does anyone have a quick function to remove duplicates?
    Im Learning !!!!

  2. #2
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: Remove Duplicates From ComboBox

    Just have a simple function like this..

    VB Code:
    1. Private Function CheckForDupe(sItem As String) As Boolean
    2.    Dim i as Integer
    3.    
    4.    CheckForDupe = False
    5.  
    6.    For i = 0 to ComboBox1.ListCount - 1
    7.        If ComboBox1.List(i) = sItem 'You might want to do " If LCase(ComboBox1.List(i)) = LCase(sItem)
    8.            CheckForDupe = True
    9.            Exit For
    10.        End If
    11.    Next i

    Then just before you add an item to a combobox, do..

    VB Code:
    1. If CheckForDupe(item) = False Then
    2.     'Add the item
    3. End If
    • If you found my post to be helpful, please rate me.

  3. #3
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Remove Duplicates From ComboBox

    Quote Originally Posted by Ricky1
    Ok my combo boxes get filled using a database on formload.

    Is there a way to see if something is already in the combo box therefore not adding it again ? (Assuming this would be slow?)

    or does anyone have a quick function to remove duplicates?

    Why not prevent them when you are loading them from the Database?

    Post your SQL String I bet that if you add Group By to it it will probably eliminate all duplicates
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Remove Duplicates From ComboBox

    using the API to find dupes in a listbox is faster than looping:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    2.     ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3.  
    4. Private Const CB_FINDSTRINGEXACT = &H158
    5.  
    6. Private Function IsDupe(ByVal sItem As String) As Boolean
    7.     IsDupe = (SendMessage(Combo1.hWnd, CB_FINDSTRINGEXACT, -1&, ByVal sItem) > -1)
    8. End Function

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Remove Duplicates From ComboBox

    Code:
    Public Sub FillStatCombos()
    
    rs.MoveFirst
        Do Until rs.EOF = True
            Combo1.AddItem rs.Fields("Type")
            Combo2.AddItem rs.Fields("Entry")
            Combo3.AddItem rs.Fields("Entrants")
            rs.MoveNext
        Loop
    
    End Sub
    is what i use to fill the combo boxes mark.




    bush does that work for a combobox ? coz u have list there.
    Im Learning !!!!

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Remove Duplicates From ComboBox

    whoops, sorry - yes it does work with a combobox, it's just a different constant.

    I have ammended the code in post #4 accordingly

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Remove Duplicates From ComboBox

    Ok and combo1 is the name of the combo box right??

    What is sItem?
    Im Learning !!!!

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Remove Duplicates From ComboBox

    well given your current code, try it like this:
    VB Code:
    1. Private Function IsDupe(ByVal cbo As ComboBox, ByVal sItem As String) As Boolean
    2.     IsDupe = (SendMessage(cbo.hWnd, CB_FINDSTRINGEXACT, -1&, ByVal sItem) > -1)
    3. End Function
    4.  
    5. Public Sub FillStatCombos()
    6.     rs.MoveFirst
    7.     Do Until rs.EOF
    8.         If Not IsDupe(Combo1, rs.Fields("Type")) Then Combo1.AddItem rs.Fields("Type")
    9.         If Not IsDupe(Combo2, rs.Fields("Entry")) Then Combo2.AddItem rs.Fields("Entry")
    10.         If Not IsDupe(Combo3, rs.Fields("Entrants")) Then Combo3.AddItem rs.Fields("Entrants")
    11.         rs.MoveNext
    12.     Loop
    13. End Sub

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Remove Duplicates From ComboBox

    There is also another technique you can use: make a string which contains all the combobox items separated by a null character. Before adding a new item you can make a simple and fast InStr check to see if the value exists already in the combobox.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim strComboItems As String
    4.  
    5. Private Sub AddCombo(ByRef Item As String)
    6.     ' make sure the combo items string has atleast a null character
    7.     ' it will remain as the first character in the string at all times
    8.     If LenB(strComboItems) = 0 Then strComboItems = vbNullChar
    9.     ' check if item already found
    10.     If InStr(strComboItems, vbNullChar & Item & vbNullChar) > 0 Then Exit Sub
    11.     ' it was not found, add it in
    12.     Combo1.AddItem Item
    13.     ' and into the string too, so the checking works
    14.     strComboItems = strComboItems & Item & vbNullChar
    15. End Sub
    16. Private Sub ClearCombo()
    17.     strComboItems = vbNullChar
    18.     Combo1.Clear
    19. End Sub
    20. Private Sub RemoveCombo(ByVal Index As Integer)
    21.     Dim lngA As Long
    22.     ' validate index
    23.     If Index < 0 Or Index >= Combo1.ListCount Then Exit Sub
    24.     lngA = InStr(strComboItems, vbNullChar & Combo1.List(Index) & vbNullChar)
    25.     strComboItems = Left$(strComboItems, lngA) & Right$(strComboItems, Len(strComboItems) - lngA - Len(Combo1.List(Index)) - 1)
    26.     Combo1.RemoveItem Index
    27. End Sub
    28. Private Sub Form_Load()
    29.     AddCombo "laa"
    30.     AddCombo "laa2"
    31.     RemoveCombo 0
    32.     AddCombo "laa2"
    33.     AddCombo "laa"
    34.     Combo1.ListIndex = 0
    35. End Sub

    I don't know how this compares against the API call; if there are a lot of duplicates, this might work faster, but if there are almost no duplicates, then this might be slower. But this certainly requires more code, no way around that. Although this can be optimized for speed if the need be, which is something you can't do with an API call

  10. #10
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Remove Duplicates From ComboBox

    Quote Originally Posted by Mark Gambo
    Why not prevent them when you are loading them from the Database?

    Post your SQL String I bet that if you add Group By to it it will probably eliminate all duplicates
    I couldn't agree more. The easiest way I've found to do this is to just query for one field at a time with the DISTINCT keyword:
    VB Code:
    1. Public Sub FillStatCombos()
    2.  
    3.     Dim oTemp As ADODB.Recordset, sSQL As String
    4.    
    5.     sSQL = "SELECT DISTINCT Type FROM Table;"   'Table is the DB table for the records.
    6.     Set oTemp = GetRecords(sSQL)
    7.     If Not oTemp Is Nothing Then
    8.         FillCombo oTemp, Combo1
    9.     End If
    10.     oTemp.Close
    11.    
    12.     sSQL = "SELECT DISTINCT Entry FROM Table;"
    13.     Set oTemp = GetRecords(sSQL)
    14.     If Not oTemp Is Nothing Then
    15.         FillCombo oTemp, Combo2
    16.     End If
    17.     oTemp.Close
    18.    
    19.     sSQL = "SELECT DISTINCT Entrants FROM Table;"
    20.     Set oTemp = GetRecords(sSQL)
    21.     If Not oTemp Is Nothing Then
    22.         FillCombo oTemp, Combo3
    23.     End If
    24.     oTemp.Close
    25.  
    26.     Set oTemp = Nothing
    27.  
    28. End Sub
    29.  
    30. Private Sub FillCombo(oRecords As ADODB.Recordset, oCombo As Object)
    31.    
    32.     Dim sTemp As String
    33.  
    34.     With oRecords
    35.         Do Until .EOF
    36.             sTemp = .Fields(1).Value & vbNullString
    37.             If Len(sTemp) <> 0 Then
    38.                 oCombo.AddItem sTemp
    39.             End If
    40.             .MoveNext
    41.         Loop
    42.     End With
    43.    
    44. End Sub
    45.  
    46. Private Function GetRecords(sQuery As String) As ADODB.Recordset
    47.  
    48.     Set GetRecords = New ADODB.Recordset
    49.    
    50.     With GetRecords
    51.         .CursorType = adOpenStatic
    52.         .CursorLocation = adUseClient
    53.         .LockType = adLockPessimistic
    54.         .Source = sQuery
    55.         .ActiveConnection = oConnect        'oConnect is a public ADODB connection
    56.         .Open
    57.     End With
    58.  
    59.     If GetRecords.RecordCount > 0 Then
    60.         GetRecords.MoveFirst
    61.     Else
    62.         GetRecords.Close
    63.         Set GetRecords = Nothing
    64.     End If
    65.  
    66. End Function
    Last edited by Comintern; Jul 11th, 2006 at 02:24 PM. Reason: Typo

  11. #11
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Remove Duplicates From ComboBox

    I prefer "Group By" because I seem to have less problems:

    VB Code:
    1. sSQL = "SELECT Type FROM Table GROUP BY Type ORDER BY Type;"   'Table is the DB table for the records.
    2.     Set oTemp = GetRecords(sSQL)
    3.     If Not oTemp Is Nothing Then
    4.         FillCombo oTemp, Combo1
    5.     End If
    6.     oTemp.Close
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  12. #12
    Junior Member
    Join Date
    Apr 2011
    Posts
    26

    Re: Remove Duplicates From ComboBox

    Quote Originally Posted by bushmobile View Post
    well given your current code, try it like this:
    VB Code:
    1. Private Function IsDupe(ByVal cbo As ComboBox, ByVal sItem As String) As Boolean
    2.     IsDupe = (SendMessage(cbo.hWnd, CB_FINDSTRINGEXACT, -1&, ByVal sItem) > -1)
    3. End Function
    4.  
    5. Public Sub FillStatCombos()
    6.     rs.MoveFirst
    7.     Do Until rs.EOF
    8.         If Not IsDupe(Combo1, rs.Fields("Type")) Then Combo1.AddItem rs.Fields("Type")
    9.         If Not IsDupe(Combo2, rs.Fields("Entry")) Then Combo2.AddItem rs.Fields("Entry")
    10.         If Not IsDupe(Combo3, rs.Fields("Entrants")) Then Combo3.AddItem rs.Fields("Entrants")
    11.         rs.MoveNext
    12.     Loop
    13. End Sub
    I know this thread is very old, but i have been surfing around the internet stuck in a similar situation and sir, your code worked a charm. !! Thanx a ton.!

  13. #13
    New Member
    Join Date
    Jan 2013
    Posts
    1

    Re: Remove Duplicates From ComboBox

    Can someone provide a mod for bushmobile's code that will solve a similar situation in Excel VBA

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Remove Duplicates From ComboBox

    Welcome to VBForums

    If bushmobile's code doesn't work for you, one of the other suggestions should. The only thing you'll miss is a little bit of speed.

  15. #15
    Addicted Member
    Join Date
    Jan 2013
    Posts
    148

    Re: Remove Duplicates From ComboBox

    I guess select distinct query would be the simplest answer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width