A problem with adding and deleting dynamic controls in a userform

keaden

New Member
Joined
Sep 29, 2010
Messages
26
Hello all, I have been tinkering about with a userform for the past week but I've come up against a slight problem.

The Userform that I have created contains both dynamically created controls and a template.

The template controls are all working fine and I have managed to get the userform to add dynamic controls with a button press (On the button press the userform adds 3 controls to the third page of the userform, 1 ComboBox, 2 TextBoxes and 3 Labels). On the next button press another set of controls are created just like the first set, but located below the previous set.

For each "Add" button press this is continued, one set of controls at a time, down the page.

My problem is this:

I have been trying to work on coding a delete button that will remove/delete each set of controls each time the delete button is pressed.

I'm sure some of you will ask why I don't just hide the controls instead but I would rather make this as tidy a job as possible and try to learn exactly why the code isn't working as it should be.

I have had some reasonable success however I think the problem lies in my looping or recording/updating my integers properly.

I have noticed when I press the "Add" button a number of times then press "delete" the button removes half the number of lines that I have added (that is to say if I press the "Add" button 4 times and then press "delete" the code removes 2 sets of lines instead of just one, if I press the "Add" button 10 times then press "delete" 5 lines are removed at once etc.)

I have been unable to work out where I'm falling down on this, so any help on this or a push in the right direction would be appreciated.

The code I have been tinkering with is below, apologies in advance for the lack of anotation or labels, I know it's bad form.

Code:
Private Sub UserForm_Initialize()

iAddedCount = 0

End Sub

Code:
Private iAddedCount As Integer

Code:
Private Sub CommandButton10_Click() 'This is the Add button
    iAddedCount = iAddedCount + 1
    Dim theCombo As Control
    Dim theTextBox800 As Control
    Dim theTextBox801 As Control
    Dim objLabel1 As Control
    Dim objLabel2 As Control
    Dim objLabel3 As Control
    
        
        Set theCombo = OSRData.MultiPage1.Pages(2).Controls.Add("Forms.Combobox.1", True)
        Set theTextBox800 = OSRData.MultiPage1.Pages(2).Controls.Add("Forms.Textbox.1", True)
        Set theTextBox801 = OSRData.MultiPage1.Pages(2).Controls.Add("Forms.Textbox.1", True)
        Set objLabel1 = OSRData.MultiPage1.Pages(2).Controls.Add("Forms.Label.1", True)
        Set objLabel2 = OSRData.MultiPage1.Pages(2).Controls.Add("Forms.Label.1", True)
        Set objLabel3 = OSRData.MultiPage1.Pages(2).Controls.Add("Forms.Label.1", True)
            
            With theCombo
                    .Name = "Combo" & iAddedCount
                    .Height = 15
                    .Left = 10
                    .Width = 300
                    .Top = 25 * iAddedCount
            End With
            
            With theTextBox800
                    .Name = "Text1" & iAddedCount
                    .Height = 15
                    .Left = 330
                    .Width = 180
                    .Top = theCombo.Top
            End With
            
            With theTextBox801
                    .Name = "Text2" & iAddedCount
                    .Height = 15
                    .Left = 520
                    .Width = 180
                    .Top = theCombo.Top
            End With
            
            With objLabel1
                    .Name = "Label100" & iAddedCount
                    .Font.Size = 8
                    .BackColor = Me.BackColor
                    .Caption = "Article"
                    .Height = 10
                    .Left = 10
                    .Width = 60
                    .Top = theCombo.Top - 10
            End With
            
            With objLabel2
                    .Name = "Label200" & iAddedCount
                    .Font.Size = 8
                    .BackColor = Me.BackColor
                    .Caption = "Observation(s)"
                    .Height = 10
                    .Left = 330
                    .Width = 60
                    .Top = theCombo.Top - 10
            End With
            
            With objLabel3
                    .Name = "Label300" & iAddedCount
                    .Font.Size = 8
                    .BackColor = Me.BackColor
                    .Caption = "Action(s)"
                    .Height = 10
                    .Left = 520
                    .Width = 50
                    .Top = theCombo.Top - 10
            End With

End Sub

Code:
Private Sub CommandButton9_Click() 'This is the Delete button
    If iAddedCount > 0 Then
    Dim theCombo As Control
    Dim theTextBox800 As Control
    Dim theTextBox801 As Control
    Dim objLabel1 As Control
    Dim objLabel2 As Control
    Dim objLabel3 As Control

          
        For Each theCombo In OSRData.MultiPage1.Pages(2).Controls
             If TypeName(theCombo) = "ComboBox" Then
                    Me.Controls.Remove "Combo" & iAddedCount
                    Me.Controls.Remove "Text1" & iAddedCount
                    Me.Controls.Remove "Text2" & iAddedCount
                    Me.Controls.Remove "Label100" & iAddedCount
                    Me.Controls.Remove "Label200" & iAddedCount
                    Me.Controls.Remove "Label300" & iAddedCount
                    iAddedCount = iAddedCount - 1
                    
            End If
        Next
End Sub

Thanks very much for looking through my code, hopefully a new set of eyes on here will point me in the right direction
K
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Here you go:

Code:
Private Sub CommandButton9_Click()
 If iAddedCount > 0 Then
    Dim theCombo As Control
          
        For Each theCombo In OSRData.MultiPage1.Pages(2).Controls
             If theCombo.Name = "Combo" & iAddedCount Then
                    Me.Controls.Remove "Combo" & iAddedCount
                    Me.Controls.Remove "Text1" & iAddedCount
                    Me.Controls.Remove "Text2" & iAddedCount
                    Me.Controls.Remove "Label100" & iAddedCount
                    Me.Controls.Remove "Label200" & iAddedCount
                    Me.Controls.Remove "Label300" & iAddedCount
                    iAddedCount = iAddedCount - 1
                    
            End If
        Next
    End If
End Sub
 
Last edited:
Upvote 0
Here you go:

Code:
Private Sub CommandButton9_Click()
 If iAddedCount > 0 Then
    Dim theCombo As Control
          
        For Each theCombo In OSRData.MultiPage1.Pages(2).Controls
             If theCombo.Name = "Combo" & iAddedCount Then
                    Me.Controls.Remove "Combo" & iAddedCount
                    Me.Controls.Remove "Text1" & iAddedCount
                    Me.Controls.Remove "Text2" & iAddedCount
                    Me.Controls.Remove "Label100" & iAddedCount
                    Me.Controls.Remove "Label200" & iAddedCount
                    Me.Controls.Remove "Label300" & iAddedCount
                    iAddedCount = iAddedCount - 1
                    
            End If
        Next
    End If
End Sub


Hello Comfy,
Thanks very much for the quick reply, much obliged.

I just tried your solution, unfortunately it didn't work. It didn't throw up any errors but the controls were not deleted.

I just wanted to double check that the line in your code

Code:
For Each theCombo In OSRData.MultiPage1.Pages(0).Controls

Should that have read:

Code:
For Each theCombo In OSRData.MultiPage1.Pages(2).Controls

In any case I have tried it with
OSRData.MultiPage1.Pages(2)
and
OSRData.MultiPage1.Pages(0)

Thanks again,
K
 
Upvote 0
Should that have read:

Code:
For Each theCombo In OSRData.MultiPage1.Pages(2).Controls

Yes, I updated the code as per your example.

Both the button name and multipage page number were different when I built a test form.

Try again with my most recent version. You probably didn't pick up that the button was called 09 instead of 9
 
Upvote 0
No, I didn't pick up on that Comfy :), thanks very much for the input, that's working fine now.
I appreciate the help.
K
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,246
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top