Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSafely manage control arrays with missing elements
Keywordscontrol array, safe
CategoriesSoftware Engineering, Tips and Tricks, Bug Alerts, Controls
 
If you try to access a control array element that doesn't exist, Visual Basic raises an error. Unfortunately a control array doesn't give you any indication of what entries exist.

Use On Error to protect against accessing elements that do not exist.

 
Private Sub Command1_Click()
Dim i As Integer

    ' Load some controls.
    For i = 1 To 5
        Load Text1(i)
    Next i

    ' Unload some in the middle of the list.
    Unload Text1(1)
    Unload Text1(3)

    ' Display the remaining controls' values.
    On Error Resume Next
    For i = Text1.LBound To Text1.UBound
        Text1(i).Tag = Text1(i).Tag
        If Err.Number = 0 Then
            Debug.Print i; Text1(i).Text
        End If
    Next i
    On Error GoTo 0

    ' Unload the remaining controls.
    On Error Resume Next
    For i = Text1.LBound To Text1.UBound
        Unload Text1(i)
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated