Microsoft KB Archive/190043

= PRB: Controls Collection vs. Standard Collection in For Each =

Article ID: 190043

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



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.



Steps to Reproduce Behavior
 Start a new Standard EXE project in Visual Basic.  Copy the following code to the code module for Form1. Private Sub Form_Load Dim x As Control Dim y As Boolean

Controls.Add "vb.commandbutton", "a" Controls.Add "vb.commandbutton", "b" Controls.Add "vb.commandbutton", "c" Controls.Add "vb.commandbutton", "d" Controls.Add "vb.commandbutton", "e" Controls.Add "vb.commandbutton", "f" For Each x In Controls Select Case x.Name Case "d", "e", "f" Controls.Remove x           End Select Debug.Print x.Name Next End Sub  Run the project. Runtime error 735 occurs.  Replace the code from step 2 with the following code.

NOTE: This code uses two separate iterations to accomplish the task. Because you can remove Control Collection elements by name, another alternative is to remove the controls without using a loop. Private Sub Form_Load Dim x As Control Dim y As Boolean

Controls.Add "vb.commandbutton", "a" Controls.Add "vb.commandbutton", "b" Controls.Add "vb.commandbutton", "c" Controls.Add "vb.commandbutton", "d" Controls.Add "vb.commandbutton", "e" Controls.Add "vb.commandbutton", "f" For Each x In Controls Select Case x.Name Case "d", "e", "f" Controls.Remove x           End Select Next For Each x In Controls Debug.Print x.Name Next

End Sub  Run the project.</li></ol>

RESULT: The names of the remaining controls appear in the Immediate window.

Keywords: kbprb KB190043

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.