PRB: Controls Collection vs. Standard Collection in For Each (190043)
The information in this article applies to:
- Microsoft Visual Basic Learning Edition for Windows 6.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
This article was previously published under Q190043 SYMPTOMS
When you remove controls from a Control collection in a For...Each loop,
you might receive the following error:
Runtime error 735: The control is no longer usable because it has been
removed from the control collection.
This occurs when you try to access the properties/methods of a control
within the same loop that you are using to remove it from the collection.
CAUSE
When you remove an element from a standard VBA Collection object inside a
loop, the iterator is updated automatically. Consequently, the collection
is aware that the element has been removed, and will not attempt to access
that member after it has been removed. The Controls collection for the
Visual Basic Form object does not exhibit this behavior, and will fail with
Runtime error 735 if you attempt to access the properties/methods of a
control after it has been removed from the collection.
RESOLUTION
To avoid Runtime error 735, do one of the following: - Remove the unwanted item(s) from the Collection before iterating over
it.
- Use two separate iterations. One to remove the unnecessary elements, and
the other to access the collection elements. See step 4 in the MORE
INFORMATION section of this article for an example.
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 5/12/2003 |
---|
Keywords: | kbprb KB190043 |
---|
|