Visvang
06-04-2010, 04:42 AM
Hi,
I am trying to create a new userform and i got most thing working, but when i insert my code to work when i press a button it gives me an err.
Here is what n got so far...
Public Sub Test()
Dim TempForm As Object
Dim NewCommandButton As MSForms.CommandButton
Dim NewLabel As MSForms.Label
Dim NewCheckBox As MSForms.CheckBox
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
With NewLabel
.Name = "FieldLabel1"
.Caption = "My Label"
.Top = 6
.Left = 30
.Width = 150
.Height = 12
.Font.Size = 7
.Font.Name = "Tahoma"
.BackColor = &H80FFFF
.Caption = "Check Me"
End With
Set NewCheckBox = TempForm.designer.Controls.Add("Forms.checkbox.1")
With NewCheckBox
.Name = "MyCheck1"
.Caption = ""
.Top = 6
.Left = 6
.Width = 12
.Height = 12
.Font.Size = 7
.Font.Name = "Tahoma"
.BackColor = &HFF00&
End With
Set NewCommandButton = TempForm.designer.Controls.Add("Forms.CommandButton.1")
With NewCommandButton
.Name = "CommandButton1"
.Caption = "OK"
.Top = 24
.Left = 6
.Width = 108
.Height = 24
End With
Set NewCommandButton = TempForm.designer.Controls.Add("Forms.CommandButton.1")
With NewCommandButton
.Name = "CommandButton2"
.Caption = "Cansel"
.Top = 24
.Left = 120
.Width = 108
.Height = 24
End With
With TempForm
.Properties("Caption") = "Carry Over..."
.Properties("Height") = 72
End With
With TempForm.codemodule
Line = .countoflines
.insertlines Line + 1, "Private Sub CommandButton2_Click()"
.insertlines Line + 2, "userform1.Hide"
.insertlines Line + 4, "End Sub"
End With
VBA.UserForms.Add(TempForm.Name).Show
End Sub
when i press 'Cansel' (CommandButton2) i get an 402 err
'Must Close Top Most modal form first'
Can anyone help me pls
I am trying to create a new userform and i got most thing working, but when i insert my code to work when i press a button it gives me an err.
Here is what n got so far...
Public Sub Test()
Dim TempForm As Object
Dim NewCommandButton As MSForms.CommandButton
Dim NewLabel As MSForms.Label
Dim NewCheckBox As MSForms.CheckBox
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
With NewLabel
.Name = "FieldLabel1"
.Caption = "My Label"
.Top = 6
.Left = 30
.Width = 150
.Height = 12
.Font.Size = 7
.Font.Name = "Tahoma"
.BackColor = &H80FFFF
.Caption = "Check Me"
End With
Set NewCheckBox = TempForm.designer.Controls.Add("Forms.checkbox.1")
With NewCheckBox
.Name = "MyCheck1"
.Caption = ""
.Top = 6
.Left = 6
.Width = 12
.Height = 12
.Font.Size = 7
.Font.Name = "Tahoma"
.BackColor = &HFF00&
End With
Set NewCommandButton = TempForm.designer.Controls.Add("Forms.CommandButton.1")
With NewCommandButton
.Name = "CommandButton1"
.Caption = "OK"
.Top = 24
.Left = 6
.Width = 108
.Height = 24
End With
Set NewCommandButton = TempForm.designer.Controls.Add("Forms.CommandButton.1")
With NewCommandButton
.Name = "CommandButton2"
.Caption = "Cansel"
.Top = 24
.Left = 120
.Width = 108
.Height = 24
End With
With TempForm
.Properties("Caption") = "Carry Over..."
.Properties("Height") = 72
End With
With TempForm.codemodule
Line = .countoflines
.insertlines Line + 1, "Private Sub CommandButton2_Click()"
.insertlines Line + 2, "userform1.Hide"
.insertlines Line + 4, "End Sub"
End With
VBA.UserForms.Add(TempForm.Name).Show
End Sub
when i press 'Cansel' (CommandButton2) i get an 402 err
'Must Close Top Most modal form first'
Can anyone help me pls