I've got two userforms - "PhaseHome" and "ModifyPhases".
I must go through the "PhaseHome" form in order to get to the "ModifyPhases" form. Once on the "ModifyPhases" form, I utilize a combo-box and button for the user to create a new & custom named userform that has a few controls. The code looks like this:
Please Note:
"Phasename" is the custom name the user entered in the earlier combo-box.
Sub New_form()
Dim Newphase As VBComponent
Dim ItemBox As MSForms.ComboBox
Dim AddItem As MSForms.CommandButton
Sheet1.Activate
'Creating the new form
Set Newphase = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With Newphase
.Properties("Height") = 250
.Properties("Width") = 350
.Properties("Caption") = Phasename
.Name = Phasename
End With
'Inserting the combobox into the dynamically created form
Set ItemBox = Newphase.Designer.Controls.Add("Forms.ComboBox.1")
With ItemBox
.Name = Phasename & "Box"
.Top = 60
.Left = 12
.Width = 140
.Height = 80
.Font.Size = 8
.Font.Name = "Tahoma"
.BorderStyle = fmBorderStyleOpaque
.SpecialEffect = fmSpecialEffectSunken
End With
'Inserting buttons into the dynamically created form
Set AddItem = Newphase.Designer.Controls.Add("Forms.commandbutton.1")
With AddItem
.Name = "cmd_1"
.Caption = "Add Line Item"
.Top = 5
.Left = 200
.Width = 110
.Height = 35
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
With that done and the userform created; I now want to add a button to the "PhaseHome" form that allows the user to get to the form we just created.
Sheet1.Select
Range("D5").Value = Range("D5").Value + 45
'Add button to Phase Home Form
Dim homeform_button As MSForms.CommandButton
Dim ufObj As UserForm
Set ufObj = ActiveWorkbook.VBProject.VBComponents("Phasehome").Designer
With ufObj
Set homeform_button = .Controls.Add("Forms.CommandButton.1")
With homeform_button
.Name = "cmd" + Phasename
.Caption = Phasename
.Top = Range("D5").Value
.Left = 45
.Width = 78
.Height = 36
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
End With
'Making sure we don't overwrite previously existing code when we insert this into PhaseHome
Dim linestart As Integer
linestart = Range("D8").Value
ThisWorkbook.VBProject.VBComponents("PhaseHome").CodeModule.InsertLines linestart, "Private Sub cmd" & Phasename & "_Click()"
linestart = linestart + 1
ThisWorkbook.VBProject.VBComponents("PhaseHome").CodeModule.InsertLines linestart, "Unload Me"
linestart = linestart + 1
ThisWorkbook.VBProject.VBComponents("PhaseHome").CodeModule.InsertLines linestart, "Sheet2.Activate"
linestart = linestart + 1
ThisWorkbook.VBProject.VBComponents("PhaseHome").CodeModule.InsertLines linestart, "" & Phasename & ".Show"
linestart = linestart + 1
ThisWorkbook.VBProject.VBComponents("PhaseHome").CodeModule.InsertLines linestart, "End Sub"
linestart = linestart + 1
Range("D8").Value = linestart
Now the good news is that this code works!.... As long as I run it from the "ModifyPhases" form directly. Once, for the very first time in a session, that I open and close the "PhaseHome" form I start receiving an error 91 (Object variable or With block Variable not set) that points to the
Set homeform_button = .Controls.Add("Forms.CommandButton.1")
line everytime I attempt to run the macro again.
Things I've tried:
I've made sure that the "PhaseHome" form is unloaded. The button that goes between the userforms always includes an Unload Me, i've also tried unloading "PhaseHome" directly within the macro itself, and also used variables tied to "PhaseHome"'s terminate and Initialize functions to ensure it is unloaded without referencing it directly.
After noticing that refreshing the workbook fixed the issue, I discovered some code online (From a source I regretfully forget) that closes and reopens the workbook each time the "ModifyPhases" form is launched which fixes the issue.
Sub CloseMe() Application.OnTime Now + TimeValue("00:00:02"), "OpenMe" ThisWorkbook.Close True End Sub
Sub OpenMe() ModifyPhases.Show End Sub
I don't know why the code tags aren't working right here.
This works but... causes corruption in the workbook and also seems rather unnecessary. Do you fellows have any theories on why this could be occurring? Thank you!
-Mano

.Designer? I think you should be able to add forms to the control directly, and I think that would (usually) be preferable. - David ZemensActiveWorkbookandThisWorkbook, when the 91 error raises, are you sure thatThisWorkbookis theActiveWorkbook? If not, then an object error is probably expected. - David ZemensThisWorkbooktoActiveworkbookdid not seem to make a difference. 2. How is it possible to add a control to an existing form without using the Designer? I don't think I've found an example that doesn't involve using the designer. 3. I do not need a collection of the controls you are correct... I modify the properties of the "custom" form at runtime but I cannot add the button to the other (PhaseHOme) form at runtime as it stands. - manofone