Combobox with all value from a MSFlexgrid column

Hello everybody,

I need your help with a code.

In my VB6 form, i have a Combo1 and a MSFLexgrid1. Once my grid populated, i want to feed my combo1 will all text from my MSFlexgrid1, column 2, without doubles.

How can i do this please?

Thanks again for your help.
LVL 11
Who is Participating?
Here is a function i made to do this. The function's arguments are the flexgrid that you are reading the data from, the column you are reading the data from, and the combo box where you are outputting the data. This way, you can use one function for many columns, many flexgrids and many combo boxes - without having to hard code different column indexes or object names.
So, if your flexgrid is named MsFlexGrid1, and you are reading from column 2 and your combo box is named Combo1, then you will call this function by:
Call addDistinctItemsToComboFromFlexGrid(MSFlexGrid1, 1, Combo1)

Private Sub addDistinctItemsToComboFromFlexGrid(fg As MSFlexGrid, fgCol As Integer, cbo As ComboBox)

'Create array to hold all the items in the flexgrid column
Dim numRowsInFG
numRowsInFG = fg.Rows
Dim str() As String
ReDim str(numRowsInFG)

'Add all items from the specified fgCol to the array
Dim i As Integer
For i = 0 To fg.Rows - 1
    str(i) = fg.TextMatrix(i, fgCol)
Next i

'Iterate the array and remove duplicates
Dim j As Long
Dim prevIndex As Long
Dim found As Boolean

prevIndex = 0
For i = LBound(str) + 1 To UBound(str)
    found = False
    For j = LBound(str) To i - 1
        If (str(i) = str(j)) Then
            found = True
            Exit For
        End If
    Next j
    If (found = False) Then
        prevIndex = prevIndex + 1
        If (prevIndex <> i) Then
            str(prevIndex) = str(i)
        End If
    End If
Next i

ReDim Preserve str(prevIndex)

'Add the distinct items to the combo box
For i = 0 To UBound(str) - 1
    Combo1.AddItem str(i)
Next i

End Sub

Open in new window

Wilder1626Author Commented:
Again, a big big thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.