Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Duplicates In A Combo Box 1

Status
Not open for further replies.

Jdoggers

Technical User
Feb 20, 2005
43
US
Hi,
I have a combo box filled with about 500 items in it. I wanted to loop through my combo box and if there is a duplicate entry in the list, i want to put that name into a listbox. My listbox will be sort of a list of duplicate entries for use later in the program. Is there any way of doing this without the second listbox having any duplicate entries in it?

thanks
 
What's the source of your combobox? If it's a query, why not just use that query as a base and have it produce the list of duplicates as the source for your listbox? If it's a table, just write a 'find duplicates' query.

Code: Where the vision is often rudely introduced to reality!
 
um...actually it is not in access, its coming from a sheet in excel. I was just thinking is there some kind of algorithm i can write that would loop through the combo and if there is a duplicate, put that name in a listbox. After that, it will ignore any more instances of that particular name but if it hits another duplicate name, it will again put the name as an .additem in the combo box...any suggestions?
 
oh im sorry...in the last post, i said that the data was coming from a spreadsheet in excel. That was wrong, i actually am taking the list from a folder on the c drive and they are file names that are duplicates. Searching through a combo box is the only way i could think of to do this...please help
 
Please post the values shown in your combobox's properties for 'rowsource' and 'rowsourcetype'. If there's a query or vba code that fills the box, please post that also.
Thank you.

Code: Where the vision is often rudely introduced to reality!
 
im sorry for all the confusion. The rowsource and rowsourcetype are both blank because im filling the combobox with code. here is some code to describe the process...combobox1 is the combobox that gets filled with all the files in the folder, without the path, or the extension, and listbox1 is the box that should be filled with nothing but a list of duplicates, but not have any redundant data. Example) lets say that i have 3 files called "dave101" in the folder, but with different extensions. this code below will fill the combobox with all the names of the files in the folder. What i need to do is take that list, and use it to make a new list in the listbox where, for example, in this case there will be one entry in the listbox that says "dave101" :



public sub fillboxes()
count2 = 0
strcount = 0


With Application.FileSearch
.NewSearch
.LookIn = "C:\myfolder\"
.SearchSubFolders = True
.Filename = "*.D*"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
End With

With Application.FileSearch
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.count & _
" file(s) found."
For i = 1 To .FoundFiles.count

'this part just is a string parsing operation, but not needed
part1 = Left(.FoundFiles(i), InStrRev(.FoundFiles(i), ".") - 1)
strcount = InStrRev(part1, "\")
part2 = Mid(part1, strcount + 1)

'this is where the items get added to the combobox
ComboBox1.AddItem part2

Next i
Else
'an error case if no files are in folder
MsgBox "There were no files found."
End If

For u = 0 To .FoundFiles.count - 1

'setfocus to the "u" position in the combobox, save it to variable
ComboBox1.ListIndex = u
part3 = ComboBox1.Text

'nested loop that looks for duplicates
For g = 0 To .FoundFiles.count - 1
ComboBox1.ListIndex = g


If part3 = ComboBox1.Text Then
count2 = count2 + 1

'if there was more than one (a duplicate entry)
If count2 > 1 Then
'add it to the list of duplicates
ListBox1.AddItem part3
count = 0
g = .FoundFiles.count - 1

End If

End If

Next g

Next u

End With
End Sub

this code almost works, but the only thing is i cant figure out how to stop the second listbox from having any duplicate entries...any help guys?
 
OK, how about adding the following code just after your 'ComboBox1.AddItem part2' statement. This will check for duplicates as you build the combobox (assume you have files sorted by name?)

If part2 <> PriorName Then ' If different, all is OK
PriorName = part2
DupCtr = 0
Else ' Found Duplicate name
If DupCtr = 0 Then ' Is it first dup?
ListBox1.AddItem part2 ' Yes, add to listbox
End If
DupCtr = DupCtr + 1
End If


Code: Where the vision is often rudely introduced to reality!
 
thank you trevil...i did exactly what you said and i also deleted all the loops at the end of the if statement where it is msgbox "no files found". It worked perfect...thanks guys i knew i could count on you...oh by the way, i gave everyone votes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top