Results 1 to 2 of 2

Thread: Removing Runtime controls using Excel 97 VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    1

    Question

    I have successfully added controls (i.e., check boxes) during runtime. I can remove
    added controls using the forms (userform1.controls collection). However, I would like
    to be able to create my on collections in order to keep track of runtime added controls
    by contol type of control purpose. I have successfully created the controls and
    colections; however, I can not use the collection I created to delete or unload the
    runtime added controls. Below you will find a simplified version of the code I am
    using. Any help would be appreciated.

    Vin
    ===============================================================================

    Dim colCheckBoxes As New Collection

    Private Sub RemoveWorks_Click()
    'NOTE: This code works. But does not use the colCheckBoxes Collection
    While UserForm1.Controls.Count > 3
    UserForm1.Controls.Remove (UserForm1.Controls.Count - 1)
    Wend

    End Sub

    Private Sub cmdAddCheckBoxes_Click()
    Dim i As Integer
    For i = 1 To 2
    'Use i as the name of the control
    strkeyname = Str(i)
    Set AddChkBox = UserForm1.Controls.Add("Forms.CheckBox.1", strkeyname, True)
    With AddChkBox
    .Caption = Trim(strkeyname)
    .FontSize = 8
    .Top = 6 + (18 * (i))
    .Width = 150
    .Left = 12
    .Height = 18
    End With
    colCheckBoxes.Add Item:=i
    Next i
    cmdAddCheckBoxes.Enabled = False
    End Sub

    Private Sub cmdRemoveCheckboxes_Click()
    'Does not work HHHHEEELLLLLPPPPPPP!!!!
    Dim i As Integer

    For i = 1 To 2
    'Use i as the name of the control
    strkeyname = Str(i)
    'Unload colCheckBoxes.Item(strkeyname)
    Set DelCheckBox = colCheckBoxes.Item(strkeyname)
    With DelCheckBox
    .SetFocus
    Unload Me
    End With
    colCheckBoxes.Remove (strkeyname)
    Next
    End Sub

  2. #2
    Guest

    Lightbulb Tip

    Instead, try this for the unloading code:

    Code:
    Dim Ctl As Control
    
    For Each Ctl In Me.Controls
      If TypeOf Ctl = "CheckBox" Then
        Unload Ctl
        Set Ctl = Nothing
      End If
    Next Ctl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width