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.
Thanks very much for looking through my code, hopefully a new set of eyes on here will point me in the right direction
K
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