Results 1 to 8 of 8

Thread: [RESOLVED] Prevent duplicates in Combo

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Resolved [RESOLVED] Prevent duplicates in Combo

    Ok, I am loading data to a form from an AccessDB via ADODB. While I am only displaying the first record, I want to populate a couple of Combo boxs also.
    Code:
    rsTable.Open "SELECT * FROM Table ORDER BY ID", CN,adOpenForwardOnly, adLockReadOnly
    txtID = rsTable!ID
    txtText1 = rsTable!Text1
    etc
    
    Do While Not rsTable.EOF
    cboFName = rsTable!FName
    cboSurname = rsTable!Surname
    etc
    rsTable.MoveNext
    Loop
    However this is causing duplicate entries in my Combo boxes, if there is more than one "Ann" in the database. Will have have to open a separate DISTINCT recordset to populate the combo boxes, or can I just check if "Ann" is already in the Combo?

    Thanks in advance
    Mel

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Prevent duplicates in Combo

    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long
    
    Private Const CB_FINDSTRING = &H14C
    
    Private Sub AddToCombobox(pstrComboBItem As String, pCB As ComboBox)
     Dim lngComboIndex As Long
        ' lngComboIndex is the ComboIndex if the item is found
        lngComboIndex = SendMessage(pCB.hwnd, CB_FINDSTRING, -1, ByVal pstrComboBItem)
        If lngComboIndex = -1 Then
            pCB.AddItem strComboBItem
        Else
            MsgBox pstrComboBItem & " is already in the Combobox"
        End If
    End Sub
    
    Private Sub Command1_Click()
    AddToCombobox "Ann", Combo1
    End Sub

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

    Re: Prevent duplicates in Combo

    Its better logic to prevent the recordset from containing any duplicates. Since the user is not manually entering in values it should suffice.

    Code:
    rsTable.Open "SELECT DISTINCT * FROM Table ORDER BY ID", CN,adOpenForwardOnly, adLockReadOnly
    That should eliminate your dups depending on yor table data.
    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Re: Prevent duplicates in Combo

    Thanks Hack will give it a go.
    RobDog888, I see your point, however there may be "Ann Ryan"; "Ann Harris"; "Ann Sullivan"; "Tony Harris" etc in the database, and all be classified as Distinct records even though there would be duplicate first names and surnames.
    Mel

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Prevent duplicates in Combo

    Quote Originally Posted by mel_flynn
    Thanks Hack will give it a go.
    RobDog888, I see your point, however there may be "Ann Ryan"; "Ann Harris"; "Ann Sullivan"; "Tony Harris" etc in the database, and all be classified as Distinct records even though there would be duplicate first names and surnames.
    Then the duplication is caused by other columns in the query, or the entire row is distinct because of primary key column. You should specify the columns to be returned.

    rsTable.Open "SELECT DISTINCT FName, Surname FROM Table ORDER BY ID", CN,adOpenForwardOnly, adLockReadOnly

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

    Re: [RESOLVED] Prevent duplicates in Combo

    "Ann Ryan"; "Ann Harris"; "Ann Sullivan"; "Tony Harris
    These would not be considered duplicates.
    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

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Prevent duplicates in Combo

    Perhaps not by us, but it is for his app it would seem.

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

    Re: [RESOLVED] Prevent duplicates in Combo

    Well going by his post it seems that the design may be flawed as hes saying people with "Ann" as a first name is a dup. when clearly having a duplicate first name is not a primary key. If that was true we all would be duplicates as there is like 6 billion people and you know there is a few with the same first names.
    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