John Gates, CISSP, CDPSE
asked on
The following code is causing random Run-Time Error '35600' Index out of bounds Ok
Dim keepplace As Integer
keepplace = lv.SelectedItem.Index
PopList
lv.ListItems(keepplace).Se
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-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Check to see if keepplace is equal to 0 and trap it if it is.
S
ASKER
or just make it equal to 1 no?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You may have a point here. If 0 means the first elemnet, then yes, make it keepplace+1, otherwise, trap for 0.
S
S
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.
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.
Another thing... this is why you delete list/array items from back-to-front when referencing items by index number.
ASKER
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-
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-
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).ind ex
Now 1
the 3
time 4
is 2
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
Now 1
the 3
time 4
is 2
ASKER
But how would you find it again and select it:
keepplace = lv.SelectedItem.Index
won't work then.
keepplace = lv.SelectedItem.Index
won't work then.
Dim keepplace_Key As String
keepplace_Key = lv.SelectedItem.Key
keepplace_Key = lv.SelectedItem.Key
ASKER
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.
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.
<<Please advise>>
If you don't supply a key to your items, there will be no key value.
If you don't supply a key to your items, there will be no key value.
ASKER
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.
ASKER