Hello everyone,
I am trying to create a dynamic UserForm in a way that the design of the form changes (textboxes and labels added) upon selecting an option from an existing ComboBox with ComboBox1_Change() event.
Let’s say the user chose “Add New Item” from the combobox. ComboBox1_Change() event will create 4 new textboxes in the form so that the user can enter the information that is needed for adding a new item into those newly created textboxes and then the information will be processed by OKButton_Click () event to add those newly added information to their respective place in a table in one of the Sheets.
The problem is that the user can chose an option they didn’t want to from the ComboBox1 as a mistake and when they choose back the correct option they originally wanted, the earlier textboxes and labels that were created by ComboBox1_Change() event (as a result of the mistake) will still be there. I want to make sure that if such a mistake & correction thingy happens the UserForm is reset to its original state (or mistakenly created textboxes and labels are removed) before the correct textboxes and labels are created so that the Form doesn’t cause any confusion for the user and the data is processed correctly.
I know that one solution could be adding a Reset button with an event like
But I want the ComboBox1_Change () event to reset the form for a smoother experience. So, I want to add a code at the begining of the ComboBox1_Change() event to make sure the Form is first reset and then the new textboxes and labels are created. So that if a mistake happens it doesn’t cause any problems. How can I do that?
I have only very very basic understanding of Excel, this is really not my area of expertise, and this document is to help my father who owns a small business with his work. So, please bare with me if this question is absurd or has a really simple answer or what I want is simply not possible with Excel VBA
Thanks in advance!
Here is a simplified version of the code of the ComboBox1_Change() event:
I am trying to create a dynamic UserForm in a way that the design of the form changes (textboxes and labels added) upon selecting an option from an existing ComboBox with ComboBox1_Change() event.
Let’s say the user chose “Add New Item” from the combobox. ComboBox1_Change() event will create 4 new textboxes in the form so that the user can enter the information that is needed for adding a new item into those newly created textboxes and then the information will be processed by OKButton_Click () event to add those newly added information to their respective place in a table in one of the Sheets.
The problem is that the user can chose an option they didn’t want to from the ComboBox1 as a mistake and when they choose back the correct option they originally wanted, the earlier textboxes and labels that were created by ComboBox1_Change() event (as a result of the mistake) will still be there. I want to make sure that if such a mistake & correction thingy happens the UserForm is reset to its original state (or mistakenly created textboxes and labels are removed) before the correct textboxes and labels are created so that the Form doesn’t cause any confusion for the user and the data is processed correctly.
I know that one solution could be adding a Reset button with an event like
VBA Code:
Private Sub ResetButton_Click()
Unload me
UserForm1.Show
End Sub
But I want the ComboBox1_Change () event to reset the form for a smoother experience. So, I want to add a code at the begining of the ComboBox1_Change() event to make sure the Form is first reset and then the new textboxes and labels are created. So that if a mistake happens it doesn’t cause any problems. How can I do that?
I have only very very basic understanding of Excel, this is really not my area of expertise, and this document is to help my father who owns a small business with his work. So, please bare with me if this question is absurd or has a really simple answer or what I want is simply not possible with Excel VBA
Thanks in advance!
Here is a simplified version of the code of the ComboBox1_Change() event:
VBA Code:
Private Sub ComboBox1_Change()
If ComboBox1.Value = "Add New Item" Then
With Me
Dim Item, ItemForeign1, ItemForeign2, ItemCode As MSForms.TextBox
Set Item = .Controls.Add("Forms.TextBox.1", "txtItem")
With Item
.Top = 54
.Left = 84
.Height = 24
.Width = 132
End With
Set ItemForeign1 = .Controls.Add("Forms.TextBox.1", "txtItemForeign1")
With ItemForeign1
.Top = 84
.Left = 84
.Height = 24
.Width = 132
End With
Set ItemForeign2 = .Controls.Add("Forms.TextBox.1", "txtItemForeign2")
With ItemForeign2
.Top = 114
.Left = 84
.Height = 24
.Width = 132
End With
Set ItemCode = .Controls.Add("Forms.TextBox.1", "txtItemCode")
With ItemCode
.Top = 144
.Left = 84
.Height = 24
.Width = 132
End With
End With
ElseIf ComboBox1.Value = "Add New Package Type" Then
With Me
Dim Package As MSForms.TextBox
Set Package = .Controls.Add("Forms.TextBox.1", "txtPackage")
With Package
.Top = 54
.Left = 84
.Height = 24
.Width = 132
End With
End With
End If
End Sub