List.Listcount property not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eleksoft
    New Member
    • Apr 2007
    • 2

    List.Listcount property not working

    My code:

    If List.Listcount > 0 then
    For intI = 0 to List.Listcount - 1
    'code
    Next intI
    End If

    For some reason, and not all the time, this loop only runs to the second last entry. I have even witnessed it in break mode and following it step by step with Shift + F8, where it reports - for example - that the listcount is 14, when if fact it is 15 and has index values of 0 to 14. I made a quick fix, but I don't like this solution as it affects my ability to bug test the rest of the sub.

    Quick and dirty fix:

    On Error Resume Next
    If List.Listcount > 0 then
    For intI = 0 to List.Listcount
    'code
    Next intI
    End If

    This way if listcount is correct, the loop will error on the last value, but will simply resume after the loop, but when it reports it incorrectly, it should still retrieve all items in the list.

    My question is why is the listcount property not reporting the correct value, if anybody else has encountered this problem, and if anybody knows a better fix for this. (ps. the ubound property is even less reliable for me)
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    It usually happens when you delete items from list.
    In this case you have to go opposite way

    Code:
    If List.Listcount > 0 then
    For intI = List.Listcount – 1 to 0 Step -1
    'code
    Next intI
    End If

    Good Luck.

    Comment

    • Esmael
      New Member
      • Feb 2007
      • 58

      #3
      Originally posted by eleksoft
      My code:

      If List.Listcount > 0 then
      For intI = 0 to List.Listcount - 1
      'code
      Next intI
      End If

      For some reason, and not all the time, this loop only runs to the second last entry. I have even witnessed it in break mode and following it step by step with Shift + F8, where it reports - for example - that the listcount is 14, when if fact it is 15 and has index values of 0 to 14. I made a quick fix, but I don't like this solution as it affects my ability to bug test the rest of the sub.

      Quick and dirty fix:

      On Error Resume Next
      If List.Listcount > 0 then
      For intI = 0 to List.Listcount
      'code
      Next intI
      End If

      This way if listcount is correct, the loop will error on the last value, but will simply resume after the loop, but when it reports it incorrectly, it should still retrieve all items in the list.

      My question is why is the listcount property not reporting the correct value, if anybody else has encountered this problem, and if anybody knows a better fix for this. (ps. the ubound property is even less reliable for me)

      Hi...

      I try to evaluate that in my PC... but i dont see any problem...
      Heres my code:

      If LstGroups.ListC ount > 0 Then
      For i = 0 To LstGroups.ListC ount - 1
      Debug.Print i
      Next i
      End If


      On my Immediate window i successfully see the output from 0-14 of 15 records... and it has no error...

      What was the error message?


      GoodLuck...

      Comment

      • eleksoft
        New Member
        • Apr 2007
        • 2

        #4
        It does not give me any error. This loop is pulling the list entries from a list that is on another form. Each time I return to my parent form and add something new to the list, it shows what I added to the list just before that item in the view form (after it again executes this loop).

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by eleksoft
          It does not give me any error. This loop is pulling the list entries from a list that is on another form. Each time I return to my parent form and add something new to the list, it shows what I added to the list just before that item in the view form (after it again executes this loop).
          If no error occurs, then your addition of On Error code will have no effect.

          The only way I can think of that this could happen is if the number of items in the list changes during the execution of the loop. Don't forget, the limits of the loop are set at the start, so if the number of items in this list decreases during execution of the loop, you will simply go past the end of the list and start returning empty strings. (Try it - List(n) just returns nothing, if n is too high.)

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Sorry, I just re-read your original post. I see you have increased the loop end and put in the On Error to catch it. But as I said, going past the end of the List() array doesn't produce an error - it simply returns no string. So the On Error will still achieve nothing.

            Comment

            Working...