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

Better Way to Check/recode Listview?

Status
Not open for further replies.

sgkdnay

Technical User
Aug 4, 2008
21
I know there is a better way, I'm a self-learner. It works fine but takes TOO long to search thru the listview and compare each other and pulls new data listview from old data listview. Is there a better way to re-code this? Code as follows:

For x = 1 To ListView3.ListItems.Count
For y = 1 To ListView4.ListItems.Count
If ListView3.ListItems.Item(x).SubItems(1) = ListView4.ListItems.Item(y).SubItems(1) Then
NA1 = True
Exit For
Else
NA1 = False
End If
Form1.Caption = "Automated CRM Distribution - " & x & "/" & y
Next y
If NA1 = False Then
NRow = ListView5.ListItems.Count + 1
ListView5.ListItems.Add
ListView5.ListItems.Item(NRow).Text = NRow
ListView5.ListItems.Item(NRow).SubItems(1) = ListView3.ListItems.Item(x).SubItems(1)
ListView5.ListItems.Item(NRow).SubItems(2) = ListView3.ListItems.Item(x).SubItems(2)
ListView5.ListItems.Item(NRow).SubItems(3) = ListView3.ListItems.Item(x).SubItems(3)
NRowA = ListView9.ListItems.Count + 1
ListView9.ListItems.Add
ListView9.ListItems.Item(NRowA).Text = NRowA
ListView9.ListItems.Item(NRowA).SubItems(1) = ListView3.ListItems.Item(x).SubItems(1)
ListView9.ListItems.Item(NRowA).SubItems(2) = ListView3.ListItems.Item(x).SubItems(2)
ListView9.ListItems.Item(NRowA).SubItems(3) = ListView3.ListItems.Item(x).SubItems(3)
NA1 = True
End If
Do: DoEvents: Loop Until EOF(x)
Label9.Caption = "Fresh Leads (" & ListView9.ListItems.Count & ")"
Next x
 
I don't know of a sort of wholesale way to populate one listview with the contents of another. However, you could improve your code, perhaps dramatically, by removing as many object references as possible from your loops. It takes much longer to evaluate a heap-based object property than a stack-based variable.

The rule is this: Whenever you need to reference from inside your loop an object property that doesn't change, you should set it equal to a variable before going into the loop, and reference the variable inside the loop.

So:
Code:
dim lv3ct as Integer, lv4ct as Integer
lv3ct = ListView3.ListItems.Count
lv4ct = Listview4.ListItems.Count
   For x = 1 To lv3ct 'note that this is evaluated each time through the loop
        For y = 1 To lv4ct 'ditto
[continue with the loop code]
Also, I would remove the references to the count properties of listview5 and 9 from inside your loop and replace them with variables.

The next thing is to make use of the With statement. When you use it, you will increase performance. So, a little further down in your loop:
Code:
            If ListView3.ListItems.Item(x).SubItems(1) = ListView4.ListItems.Item(y).SubItems(1) Then
                NA1 = True
                Exit For
            Else
                NA1 = False
            End If
        Form1.Caption = "Automated CRM Distribution - " & x & "/" & y
        Next y
        If NA1 = False Then
            [COLOR=red]With ListView5.ListItems[/color]
                NRow = .Count + 1
                .Add
                [COLOR=red]With .Item(Nrow)[color] 'note that this is a with block within a with block, you can do this
                    .Text = NRow
                    .SubItems(1) = ListView3.ListItems.Item(x).SubItems(1)
                    .SubItems(2) = ListView3.ListItems.Item(x).SubItems(2)
                    .SubItems(3) = ListView3.ListItems.Item(x).SubItems(3) 
                    [COLOR=red]End With
                End With
                [With Listview9.listItems and so on][/color]
            NRowA = ListView9.ListItems.Count + 1
            ListView9.ListItems.Add
            ListView9.ListItems.Item(NRowA).Text = NRowA
            ListView9.ListItems.Item(NRowA).SubItems(1) = ListView3.ListItems.Item(x).SubItems(1)
            ListView9.ListItems.Item(NRowA).SubItems(2) = ListView3.ListItems.Item(x).SubItems(2)
            ListView9.ListItems.Item(NRowA).SubItems(3) = ListView3.ListItems.Item(x).SubItems(3)
            NA1 = True
        End If
        [COLOR=green]'Do: DoEvents: Loop Until EOF(x)[/color]
        Label9.Caption = "Fresh Leads (" & ListView9.ListItems.Count & ")" [COLOR=green]'put the count in a variable here too[/color]
        [COLOR=red]DoEvents[/color]
    Next x

The with statement addresses the problem of repetitive evaluations of the same object pointer in a single context. If you enclose all the references in a with block, they only get evaluated once per block. So, in this case, ListView5.ListItems gets evaluated once instead of 6 times.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top