Combobox duplicates

db , vb6 South Africa
  • 6 years ago

    Hi

    How can I remove duplicates that are displayed in a combo box when the user clicks on it.

     

    Thanks 

  • 6 years ago

     Private Sub Combo1_Click()
        
        Dim sTMP As String
        Dim i As Integer

        sTMP = Combo1.Text

        For i = Combo1.ListCount - 1 To 0 Step -1
            If Combo1.List(i) = sTMP And Combo1.ListIndex <> i Then Combo1.RemoveItem i
        Next i

    End Sub


  • 6 years ago

    Hi

    This code doesn't seem to be the solution. I need it to clear all the duplicates in a combo box that maybe there in the gotfocus event of the combo box.

     

    Thanks 

     

  • 6 years ago

    Try this,

    Private Sub Combo1_GotFocus()
     
    Dim i As Integer, n As Integer

    For i = 0 To Combo1.ListCount - 1
        For n = Combo1.ListCount - 1 To 0 Step -1
            If i <> n Then
                If Combo1.List(n) = Combo1.List(i) Then Combo1.RemoveItem n
            End If
        Next
    Next

    Me.Refresh
    Combo1.Refresh

    End Sub

     

  • 6 years ago

    Thanks it works but seems to be slow. Do you perhaps know how I could possibly remove them in the Access database before loading into the combobox?

     

    Thanks 

  • 6 years ago

    In SQL Statement use distinct keyword....

     Ex: Suppose you are retreiving StudentTowns from Student Table. Then the SQL Statement will be like this...

    Select distinct StudentTowns from Students.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler