VB6 - Feed Listbox with distinct value from MSHFlexgrid2 column 16

Hi

I have this MSHFlexgrid2 populated with more than 1000 rows of data.

I would like to create a command button that will feed my List1 with distinct values from column 16 of my grid, and also orders by  numbers.

How can i do that?

The values format i have in my grid column 16 looks like this:
000-00-0000
111-11-1111


How can I do that?

Thanks for your help
LVL 11
Wilder1626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Can you post a zip of the project please?
Wilder1626Author Commented:
Hi MartinLiss

Here is a sample. In column 16, you will found values, some in duplicates, but only single values should show in the List1 checkbox style.
ListBox-sample.zip
Martin LissOlder than dirtCommented:
Private Sub cmdDistinct_values_Click()()
Dim col16 As New Collection
Dim lngIndex As Long

On Error Resume Next ' Ignore error generated by attempt to add duplicate value
For lngIndex = 1 To MSHFlexGrid2.Rows - 1
    col16.Add MSHFlexGrid2.TextMatrix(lngIndex, 16), CStr(MSHFlexGrid2.TextMatrix(lngIndex, 16))
Next
On Error GoTo 0

List1.Clear
For lngIndex = 1 To col16.Count
    List1.AddItem col16(lngIndex)
Next
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

aikimarkCommented:
My preference for uniqueness is to use a dictionary object
Private Sub cmdDistinct_values_Click()
    Dim dicUnique As Object
    Dim vItem As Variant
    
    Set dicUnique = CreateObject("scripting.dictionary")
    
    For lngIndex = 1 To MSHFlexGrid2.Rows - 1
        vItem = CStr(MSHFlexGrid2.TextMatrix(lngIndex, 16))
        If dicUnique.exists(vItem) Then
        Else
            dicUnique(vItem) = 1
        End If
    Next
    
    List1.Clear
    For Each vItem In dicUnique
        List1.AddItem vItem
    Next
End Sub

Open in new window

Martin LissOlder than dirtCommented:
The result of a test where clicking the button causes the sub to be run 1000 times yields these results on my PC where Test1 is my code and Test2 is aikimark's. I clicked the button 4 times for each set of code. The results are in milliseconds timed using the method described in my How to Time Code article.

Test 1: 125
Test 1: 141
Test 1: 140
Test 1: 141

Test 2: 312
Test 2: 281
Test 2: 281
Test 2: 453

I should also say that you most likely won't notice the difference and that referring to a particular item is often easier with dictionaries than with collections but here we are just writing it all to the list so that doesn't come into play.
aikimarkCommented:
Thanks for testing, Martin.
Martin LissOlder than dirtCommented:
You're welcome.
Wilder1626Author Commented:
HI again

I like both options, but i do see a difference with MartinLiss version due to the number of record i have.

But this is exactly what i was looking for. Thanks again.
Martin LissOlder than dirtCommented:
You're welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.