Results 1 to 9 of 9

Thread: check for duplicate items in combobox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    check for duplicate items in combobox

    hi,

    I have this combobox with these items

    English
    English
    Math
    Phisics
    Math

    These items are loaded from an access database. I want only one of each.
    how do i check for duplicate items in a combobox, and if there are duplicate items, how do i remove them?


    Thanks.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: check for duplicate items in combobox

    I'm curious - where did the duplicate row come from - what is the SQL you are using to get the recordset.

    One of our 10 commandments in our shop is to "never use SELECT DISTINCT"...

  3. #3
    Fanatic Member
    Join Date
    Dec 2004
    Posts
    610

    Re: check for duplicate items in combobox

    can you give an example for the use of distinct and then adding it into the combo box?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    Re: check for duplicate items in combobox

    thanks for the reply,

    but that is not what i am looking for, because i do need the other data i the datarow

    my access table looks like this

    Code:
    ID      Subject     Year             Mark      Date              Description
    1       English      2005-2006     6.0        12-12-2005     Just A description
    2       English      2005-2006     7.5        01-03-2006     Another Description
    3       Math        2005-2006     8.0        02-03-2006     Description
    4       Physics     2005-2006     7.2        15-03-2006     Just a Description
    5       Math        2005-2006     6.8        23-04-2006      Description
    And these entry are loaded in the combobox with this code

    first i create a dataset
    VB Code:
    1. Private Sub DataSetCreateSubject()
    2.         Dim oAdaptor As OleDbDataAdapter
    3.         Dim strSql As String
    4.         Dim strConn As String
    5.  
    6.         'Get Connection String
    7.         strConn = ConnectionString()
    8.  
    9.         'Build SQL String
    10.         strSql = "SELECT * FROM cijfers WHERE Year='" & cbYear.SelectedItem.ToString() & "' ORDER BY ID ASC"
    11.  
    12.         moDS = New DataSet
    13.  
    14.         Try
    15.             'Create new Data Adapter
    16.             oAdaptor = New OleDbDataAdapter(strSql, strConn)
    17.             'Fill Dataset from Adaptor and give it a name
    18.             oAdaptor.Fill(moDS, "Subject")
    19.             'Create a Primary Key
    20.             With moDS.Tables("Subject")
    21.                 .PrimaryKey = New DataColumn() _
    22.                 {.Columns("ID")}
    23.                 .Columns("ID").AutoIncrement = True
    24.                 .Columns("ID").AutoIncrementStep = 1
    25.             End With
    26.  
    27.         Catch ex As Exception
    28.             MessageBox.Show(ex.Message)
    29.  
    30.         End Try
    31.     End Sub

    then i load in the combobox

    VB Code:
    1. Private Sub ListLoadVakken()
    2.         Dim oItem As listYears
    3.         Dim oRow As DataRow
    4.  
    5.         cbSubject.Items.Clear()
    6.         ' Loop through each row and get a DataRow
    7.         For Each oRow In moDS.Tables("Vak").Rows
    8.             ' Create New Item to hold PK and Description
    9.             oItem = New listYears
    10.             With oItem
    11.                 .ID = CInt(oRow.Item("ID"))
    12.                 .Value = oRow.Item("Vak").ToString()
    13.             End With
    14.  
    15.             ' Add Item to list box
    16.             cbSubject.Items.Add(oItem)
    17.         Next
    18.  
    19.  
    20.         If cbSubject.Items.Count = 0 Then
    21.             MessageBox.Show("Er zijn nog geen vakken voor dit jaar toegevoegd. Voeg eerst een vak toe.")
    22.             cbSubject.Text = ""
    23.         Else : cbSubject.SelectedIndex = 0
    24.         End If
    25.  
    26.     End Sub


    So i am really looking for a way to search to combobox on duplicate items and then delete them

    Thanks

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: check for duplicate items in combobox

    Quote Originally Posted by kulrom
    but why ... i've used DISTINCT many times before and i never noted any issue about that ... then i'm wondering why you recommend not using of DISTINCT?
    DISTINCT and GROUP BY have a place in SQL - that's for sure.

    But I've seen too many people develop a poor join - resulting in "phantom" duplicate rows and then using DISTINCT to get rid of them.

    In the past 3 years we have developed nearly 1000 STORED PROCEDURES and I cannot remember the time we used SELECT DISTINCT.

    Prior to MS SQL SERVER 2000, SELECT DISTINCT would build the resultset and then apply the DISTINCT upon it - GROUP BY was better. Now they are the same.

    And as you can see from the reply - DISTINCT isn't going to work in this case either.

    As for the DUPLICATE issue in the COMBO box - search around the FORUM, I've seen API tricks on COMBOBOXES that get rid of duplicates...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    Re: check for duplicate items in combobox

    Quote Originally Posted by szlamany
    As for the DUPLICATE issue in the COMBO box - search around the FORUM, I've seen API tricks on COMBOBOXES that get rid of duplicates...
    I searched the forums, but i can't find it. what keyword should i use?

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: check for duplicate items in combobox

    Quote Originally Posted by digita
    I searched the forums, but i can't find it. what keyword should i use?
    It was tough to find - but here's a link:

    Find a string in a combobox with API

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    Re: check for duplicate items in combobox

    what does it do, because i don't inderstand it, and how do i use it?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    4.                                                                         ByVal wMsg As Long, _
    5.                                                                         ByVal wParam As Long, _
    6.                                                                         lParam As Any) _
    7.                                                                         As Long
    8.  
    9. Private Const CB_ERR = (-1)
    10. Private Const CB_FINDSTRING = &H14C
    11. Private Const CB_FINDSTRINGEXACT = &H158
    12.  
    13. Private Function FindItemByString(cmb As ComboBox, ByVal SearchFor As String, Optional FindExact As Boolean = False) As Integer
    14.     FindItemByString = CInt(SendMessage(cmb.hwnd, IIf(FindExact, CB_FINDSTRINGEXACT, CB_FINDSTRING), _
    15.                             CB_ERR, ByVal SearchFor))
    16. End Function

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: check for duplicate items in combobox

    Hey, that looks very simlar to my code I posted a long time ago.

    What it does is use the sendmessage api to find in the passed combo box (depending on the exact or
    like parameter being passed) a item with the same (exact or partial matching) combo item text. Then if it
    finds it it will return the index integer of the found item.


    VB Code:
    1. 'Usage for exact match:
    2. Private Sub Command1_Click()
    3.     Combo1.ListIndex = FindItemByString(Combo1, Combo1.Text, True)
    4. End Sub
    If its not found then the function returns a -1 which will not give the combo an error but rather a no
    selection depending on the Style of the combo box.

    HTH
    Last edited by RobDog888; Feb 13th, 2005 at 01:12 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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