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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic Question

Status
Not open for further replies.

CasperTFG

Programmer
Nov 15, 2001
1,210
US
Okay my brain is jsut fried on this one... I probably started from the worng angle and have totaly confused myself now. Does anyone else have a better idea on how to do this.

I have a list box with entries that I need to save. Duplicates are allowed in the list, but I cannot duplicate the entries on the save. So starting with this.

A
B
B
D
A
C
A
and I want it to look Like this when I save it...

A(1)
A(2)
A(3)
B(1)
B(2)
C
D
Note that C and D would not be numbered as they are Unique. I would post the code that I have so far, but as I said it is probably started from the wrong angle, so I want to see if there are any Fresh thoughts out there.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
As a start sort your list box. This should help you then figure out the logic.
 
Got that started...

Okay here is what I have... But it is buggy very buggy.
Code:
            With lvw
                iTestCount = .ListItems.Count
                .SortOrder = lvwAscending
                .SortKey = 0
                .Sorted = True
                .Refresh
                iNameIdx = 1
                sLastName = .ListItems(1).Text
                If sLastName = .ListItems(2).Text Then
                    .ListItems(1).Text = sLastName & "(" & iNameIdx & ")"
                    iNameIdx = iNameIdx + 1
                End If
                For iTestLoop = 2 To iTestCount
                    If sLastName = .ListItems(iTestLoop).Text Then
                        .ListItems(iTestLoop).Text = sLastName & "(" & iNameIdx & ")"
                        iNameIdx = iNameIdx + 1
                    Else
                        sLastName = .ListItems(iTestLoop).Text
                        iNameIdx = 1
                        If sLastName = .ListItems(iTestLoop + 1).Text Then
                            .ListItems(iTestLoop).Text = sLastName & "(" & iNameIdx & ")"
                            iNameIdx = iNameIdx + 1
                        End If
                    End If
                Next iTestLoop
            End With

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
Here is another alternative if you don't mind using an ADO recordset object:

Code:
    Dim LCV As Long
    Dim count As Long
    
    Dim rs As Recordset
    Set rs = New ADODB.Recordset
    
    rs.Fields.Append "ValueToSave", adVarChar, 10
    rs.Fields.Append "DisplayValue", adVarChar, 10
    rs.Open
    
    For LCV = 0 To List1.ListCount - 1
        rs.Filter = "ValueToSave='" & List1.List(LCV) & "'"
        Select Case rs.RecordCount
            Case 0
                rs.AddNew
                rs("ValueToSave") = List1.List(LCV)
                rs("DisplayValue") = List1.List(LCV)
            Case 1
                rs.MoveFirst
                rs.Find "ValueToSave='" & List1.List(LCV) & "'"
                rs("DisplayValue") = List1.List(LCV) & "(1)"
                rs.Update
                
                rs.AddNew
                rs("ValueToSave") = List1.List(LCV)
                rs("DisplayValue") = List1.List(LCV) & "(" & rs.RecordCount & ")"
            Case Else
                rs.AddNew
                rs("ValueToSave") = List1.List(LCV)
                rs("DisplayValue") = List1.List(LCV) & "(" & rs.RecordCount & ")"
        End Select
    Next
    
    'No go back through and print them off
    rs.Sort = "DisplayValue"
    rs.Filter = ""
    rs.MoveFirst
    
    Do While Not rs.EOF
        Debug.Print rs("DisplayValue")
        rs.MoveNext
    Loop
 
I added a few more ifs and got it fixed...

Bah what a mess. I wish there were a more elegant way to do it.

Thanks,

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
There is. Just create a two dimensional array. Sort your list box, plug the values into the array. As you run through the array, iterate a counter, plug its value into the second dimension. Every time you come to a new value, reset the counter to 0. Then save the array instead of the list box.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top