The following code is causing random Run-Time Error '35600'  Index out of bounds Ok

John Gates, CISSP
John Gates, CISSP used Ask the Experts™
on
       

    Dim keepplace As Integer

        keepplace = lv.SelectedItem.Index
   
        PopList

    lv.ListItems(keepplace).Selected = True

This is the code that I use to at a 5 minute interval refresh the items in a listview control.  I cannot reproduce the problem but I have complaints that it is happening.  Anyone ever run into this problem?

I am unsure as to why an error like that would ever come up.  The list is never empty.

Thanks in advance!
-D-
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
>        keepplace = lv.SelectedItem.Index
first ensure/check if you HAVE a selected item:

if lv.SelectedItem is nothing then
  'no selected item
  keepplace = 0
else
  keepplace = lv.SelectedItem.Index
end if



and to ensure the following line does not error out:
on error resume next
    lv.ListItems(keepplace).Selected = True

or, if you can be sure the the item (keepplace) is not removed from the list:

if keepplace <> 0 then
    lv.ListItems(keepplace).Selected = True
end if
John Gates, CISSPSecurity Professional

Author

Commented:
Good call.  I will put these changes in place and let you know in a couple days 8)
Top Expert 2004

Commented:
This error will occurs if keepplace is = to 0. That is because listitems cannot begin with a 0. It always begins with 1.
Check to see if keepplace is equal to 0 and trap it if it is.

S
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

John Gates, CISSPSecurity Professional

Author

Commented:
or just make it equal to 1 no?
Top Expert 2004
Commented:
No. What you need to do is to check the value of keepplace and trap it if it is = to 0

If keepplace > 0 then
      lv.ListItems(keepplace).Selected = True
Else
     'whatever you want to do: ignore.... do something.... msgbox... etc....
End If

S
Top Expert 2004

Commented:
You may have a point here. If 0 means the first elemnet, then yes, make it keepplace+1, otherwise, trap for 0.

S
Top Expert 2014

Commented:
If it is possible that your users or (other) application code is removing nodes/rows, you will also experience this error on a situational basis.

You might make your application more resilient by using the key of the node instead of its index.  This is analogous to using a recordset bookmark instead of the absoluteposition property.
Top Expert 2014

Commented:
Another thing... this is why you delete list/array items from back-to-front when referencing items by index number.
John Gates, CISSPSecurity Professional

Author

Commented:
Quote>
 If it is possible that your users or (other) application code is removing nodes/rows, you will also experience this error on a situational basis.

You might make your application more resilient by using the key of the node instead of its index.  This is analogous to using a recordset bookmark instead of the absoluteposition property.
</quote>
Absolutely,  Can you explain how to do this "using the key of the node instead of its index"

-D-
Top Expert 2014

Commented:
Key is a property of a node and must be unique within a listview or treeview.  It is usually assigned a value when you create the node.  The control hashes the key string value and creates a numeric value that is used in a lookup table.

If you can derive some unique value from the row you are adding, it will make your job easier.

==========================
Example:
    ListView1.ListItems.Add , "Now", "Now"
    ListView1.ListItems.Add , "is", "is"
    ListView1.ListItems.Add , "the", "the"
    ListView1.ListItems.Add , "time", "time"

iterating through the listitems:
Debug.Print listview1.ListItems(x).Key, listview1.ListItems(x).index

Now            1
the            3
time           4
is             2
John Gates, CISSPSecurity Professional

Author

Commented:
But how would you find it again and select it:

keepplace = lv.SelectedItem.Index

won't work then.
Top Expert 2014

Commented:
Dim keepplace_Key As String
keepplace_Key = lv.SelectedItem.Key
John Gates, CISSPSecurity Professional

Author

Commented:
Using your exact code snippet above:

Dim keepplace_Key As String
keepplace_Key = lv.SelectedItem.Key

MsgBox keepplace_Key

The message box is empty when an item is selected....  Please advise.
Top Expert 2014

Commented:
<<Please advise>>

If you don't supply a key to your items, there will be no key value.
John Gates, CISSPSecurity Professional

Author

Commented:
The items come from a database.  There is an ID field but it is hidden from the list.  So I will make my decision as such.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial