ensuring unique items in combobox/listbox 
Author Message
 ensuring unique items in combobox/listbox

how can i go about ensuring that i'm only adding unique items into a list or
combobox? I have a VBA macro that processes a series of files and populates
a combobox with a list of items found in each file. It's quite likely that
more than one file will have multiple items the same and i'd like to limit
the combobox to only unique values.

i've tried doing what's listed below but i'm still getting the error thrown
when it can't set the text attribute.

is there a simpler way?

in my code some place

sub .........
..........
..........
on error goto addunique
cbxMyComboBox.Text = "itemtoadd"
on error goto 0

exit sub

addunique:
cbxmycombobox.additem = "itemtoadd"
resume next

--

Save The Whales - Collect The Whole Set!



Sat, 01 Jan 2005 20:33:15 GMT  
 ensuring unique items in combobox/listbox
I thought i'd mention that i have this working just fine for a listbox
control. am i going too far to expect the combobox to allow this sort of
checking as well?

this is the code for the listbox

private sub foo()
    .
    . ' get a selection of items here
    .
    If UBound(arr) = 0 Then
        On Error GoTo addsingle
        lbxFlsBatch.Text = LCase(arr(0))
        On Error GoTo 0
    Else

        Dim i As Integer
        For i = 1 To UBound(arr)
            On Error GoTo addmulti
            lbxFlsBatch.Text = LCase(arr(0) & arr(i))
            On Error GoTo 0
        Next

    End If
    lbxFlsBatch.TopIndex = lbxFlsBatch.ListCount - 1
skip:
    Exit Sub
addsingle:
    lbxFlsBatch.AddItem LCase(arr(0))
    Resume Next
addmulti:
    lbxFlsBatch.AddItem LCase(arr(0) & arr(i))
    Resume Next
End Sub

--

Save The Whales - Collect The Whole Set!



Sat, 01 Jan 2005 20:42:53 GMT  
 ensuring unique items in combobox/listbox
G'day Eamon Millman,

each time you go to add an entry, cycle through the existing entries
and if there's a match set a flag and exit the loop.
If the flag's set then don't add the new item.
You may also want to bubblesort this list before adding it to the box.

Some example code for both of these can be obtained from my dev set as
per my blog ( http://www.*-*-*.com/ ) > Free Products.


this:

Quote:
>how can i go about ensuring that i'm only adding unique items into a list or
>combobox? I have a vba macro that processes a series of files and populates
>a combobox with a list of items found in each file. It's quite likely that
>more than one file will have multiple items the same and i'd like to limit
>the combobox to only unique values.

>i've tried doing what's listed below but i'm still getting the error thrown
>when it can't set the text attribute.

>is there a simpler way?

>in my code some place

>sub .........
>..........
>..........
>on error goto addunique
>cbxMyComboBox.Text = "itemtoadd"
>on error goto 0

>exit sub

>addunique:
>cbxmycombobox.additem = "itemtoadd"
>resume next

Steve Hudson - Word Heretic, Sydney, Australia
{*filter*} teacher, trainer, tutor, writer, developer

You agree by writing to me personally that any material can be reused publicly unless you explicitly disclaim it. (For List and blog use.)


Sun, 02 Jan 2005 11:24:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. HowTo ensure uploaded file filenames have unique names?

2. Reference ComboBox item to fill ListBox

3. Randomly list items in listbox/combobox

4. It is possible to create a ListBox and combobox With Images in the items

5. It is possible to create a ListBox and combobox With Images in the items

6. Randomly list items in listbox/combobox

7. Move ListBox items to another ListBox

8. Listbox with single character item corrupts item?

9. populating vb.net listbox from selected items of another listbox

10. Listbox items: copy to another listbox, but randomize their order

11. Access 97 - Counting unique items

12. count unique items in columnB against ColumnA - help to write a function

 

 
Powered by phpBB® Forum Software