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).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-
LVL 18
John Gates, CISSPSecurity ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>        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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Good call.  I will put these changes in place and let you know in a couple days 8)
ShauliCommented:
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
Determine the Perfect Price for Your IT Services

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

John Gates, CISSPSecurity ProfessionalAuthor Commented:
or just make it equal to 1 no?
ShauliCommented:
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
ShauliCommented:
You may have a point here. If 0 means the first elemnet, then yes, make it keepplace+1, otherwise, trap for 0.

S
aikimarkCommented:
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.
aikimarkCommented:
Another thing... this is why you delete list/array items from back-to-front when referencing items by index number.
John Gates, CISSPSecurity ProfessionalAuthor 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-
aikimarkCommented:
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 ProfessionalAuthor Commented:
But how would you find it again and select it:

keepplace = lv.SelectedItem.Index

won't work then.
aikimarkCommented:
Dim keepplace_Key As String
keepplace_Key = lv.SelectedItem.Key
John Gates, CISSPSecurity ProfessionalAuthor 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.
aikimarkCommented:
<<Please advise>>

If you don't supply a key to your items, there will be no key value.
John Gates, CISSPSecurity ProfessionalAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.