|
-
Mar 13th, 2008, 09:44 AM
#1
Thread Starter
Frenzied Member
[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
-
Mar 13th, 2008, 09:49 AM
#2
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
-
Mar 13th, 2008, 01:12 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 14th, 2008, 05:01 AM
#4
Thread Starter
Frenzied Member
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.
-
Mar 14th, 2008, 05:06 AM
#5
Re: Prevent duplicates in Combo
 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
-
Mar 14th, 2008, 12:59 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 14th, 2008, 01:28 PM
#7
Re: [RESOLVED] Prevent duplicates in Combo
Perhaps not by us, but it is for his app it would seem.
-
Mar 14th, 2008, 01:43 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|