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!

Listboxes to Flexgrid Logic 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
I have been going round in circles on this one. I have 2 listboxes which are syncronised (ie married data listrow for listrow)

List1 contains: List2 Contains
Lawyer Ben Adams 1
Doctor Jane Temp 1
Painter Tom James 1
Painter Dick Smith 1
Lawyer Sam Taylor 2
Painter Tom James 2

The numbers in list 2 are itemdata, and refer to columns in
a flexgrid for which I want asterisks inserted.
Flexgrid has 4 columns
The Flegrid when filled from above should look like:

Lawyer Ben Adams *
Lawyer Sam Taylor *
Doctor Jane Temp *
Painter Tom James * *
Painter Dick Smith *

How in hells name do I get there? Thanks
 
I think the problem is because some names appear more than once.
To create list of distinct names, use a collection.
Here is one way, collection holds row index (1...5) for each person:
colRows.Item("Ben Adams") = 1
...
colRows.Item("Sam Taylor") = 5
Code:
' Form1, add 2 listboxes and msflexgrid(FixedCols = 1, Cols = 5)
Option Explicit

Private Sub Form_Click()
    Dim colRows As Collection
    Dim i As Long, numrows As Long, tst As Long
    On Error Resume Next
    
    Set colRows = New Collection
    
    For i = 0 To List2.ListCount - 1
        ' get row index for the person
        tst = colRows.Item(List2.List(i))
        If Err Then
            Err.Clear
            ' item does not exist, add it to collection
            numrows = numrows + 1
            colRows.Add numrows, List2.List(i)
            tst = numrows
            ' add to flexgrid
            Me.MSFlexGrid1.AddItem vbTab & List1.List(i) & vbTab & List2.List(i)
        End If
        ' insert asterisk
        If List2.ItemData(i) > 0 Then
            Me.MSFlexGrid1.Row = tst
            Me.MSFlexGrid1.Col = 2 + List2.ItemData(i)
            Me.MSFlexGrid1.Text = "*"
        End If
    Next
End Sub
 
You could also add this line at start of Form_Click, to clear the grid:

Me.MSFlexGrid1.Rows = 1
 
Thanks a lot ameba, you caught me just before working away for a week. I will try it out while away. It looks good and worthy of a star for the help. Many thanks indeed, its been a real headache. Also, never considered using the tab function across a grid, always stuck it's location in. Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top