Brian Babo
Brian Babo

Reputation: 41

How to remove dynamically added userform controls one at a time?

I'm building a workout logging application that allows the user to add a movement, number of reps, and the associated weight while the userform is running. (You can also think of it as "order-picking" eg. "3" "red" "pencils", "2" "blue" "markers", etc.)

The code adds the two textboxes and the combo box when the "+" button is clicked.

When the "-" button is clicked, only the last added group of controls will be deleted but if the "-" button is clicked again I'll get the "Catastrophic Failure" error message.

I'm pretty sure these controls need to be deleted using controls.remove (object).name but once that last group is deleted, I'll need to update the object variable to the next group that could be removed which I'm struggling with.

I found similar questions on here with answers, but those deleted all userform controls. I only want to remove the last group added.

enter image description here

Option Explicit
Public movementCounter As Integer
Public repsTextBox As Object
Public movementComboBox As Object
Public weightTextBox As Object
Public cntrlsColl As Collection

Private Sub addMvmtCmndButt_Click()

    movementCounter = movementCounter + 1

    Set repsTextBox = Controls.Add("Forms.TextBox.1")
    Set movementComboBox = Controls.Add("Forms.ComboBox.1")
    Set weightTextBox = Controls.Add("Forms.TextBox.1")

    With repsTextBox
        .Name = "RepsTextBox" & movementCounter
        .Height = 18
        .Width = 45
        .Left = 10
        .Top = 30 * (movementCounter - 1) + 5
    End With

    With movementComboBox
        .Name = "MovementComboBox" & movementCounter
        .Height = 18
        .Width = 90
        .Left = 65
        .Top = 30 * (movementCounter - 1) + 5
        .RowSource = listsSht.Range("movementList").Address
    End With

    With weightTextBox
        .Name = "WeightTextBox" & movementCounter
        .Height = 18
        .Width = 45
        .Left = 165
        .Top = 30 * (movementCounter - 1) + 5
    End With

End Sub

Private Sub deleteMvmtCmndButt_Click()

'Works, but only for the most recently added group of controls
    With Me.Controls
        .Remove repsTextBox.Name
        .Remove movementComboBox.Name
        .Remove weightTextBox.Name
    End With

'update what the next group of controls will be on-deck
'    If movementCounter > 0 Then
'        repsTextBox.Name = "RepsTextBox" & movementCounter
'        movementComboBox.Name = "MovementComboBox" & movementCounter
'        weightTextBox.Name = "WeightTextBox" & movementCounter
'    End If

End Sub

Upvotes: 1

Views: 1066

Answers (1)

Domenic
Domenic

Reputation: 8114

Try the following code...

Private Sub deleteMvmtCmndButt_Click()

    If movementCounter > 0 Then
        With Me.Controls
            .Remove "RepsTextBox" & movementCounter
            .Remove "MovementComboBox" & movementCounter
            .Remove "WeightTextBox" & movementCounter
        End With
        movementCounter = movementCounter - 1
    End If

End Sub

Hope this helps!

Upvotes: 1

Related Questions