L
Ludo
Hi,
I have a userform (frmProfileGraph) where I add some controls
programatically (see example:
http://spreadsheetpage.com/index.php/site/tip/creating_a_userform_programmatically/)
, the number of controls is depending on the number of executed test,
so this one is variable.
Thats why i want to add them programmatically and not fixed.
The controls i add are labels and checkboxes.
So far it works fine, but i want to add programatically some code for
the checkboxes when clicking on a checkbox (change event) that
depending on the TRUE / FALSE condition a routine is executed. And
here it fails.
The code for the checkboxes is added to the codemodule (see below the
Userform initialize code) , but i get an Automation error as follow:
Run-time error '-214717848(80010108)':
Automation error
The object invoked has disconnected from its clients.
I hoped to get some information by adding a errorhandler in the code,
but this isn't invoked. I get the Automation error before the
errorhandler is executed.
Here's my code:
Private Sub UserForm_Initialize()
Dim MyLabel As MSForms.Label
Dim MyCheckbox As MSForms.CheckBox
Dim i As Integer
Dim MaxWidth As Long 'max width for the labels
Const LabelOffset = 16 'offset between the different FMT test
labels
Const TopOffset = 140
'if a graph has been made before, clear the combobox
If GraphMade = True Then
Me.lbFMT_LogList.Clear
End If
If StartUp = True Then
'add labels with the FMT Test name
For i = 1 To UBound(FMT_TestNames)
Set MyLabel = Me.Controls.Add("Forms.Label.1")
With MyLabel
.Name = "lbl_TestName" & i
.Font.Bold = True
.Font.Size = 10
.Font.Name = "Tahoma"
.WordWrap = False
.Width = 102
.Height = 36
.Top = TopOffset + (i * LabelOffset)
.Left = 2
.Caption = FMT_TestNames(i)
.Visible = True
If .Width > MaxWidth Then
MaxWidth = .Width
End If
End With
Next
'add labels to place failure count in it
For i = 1 To UBound(FMT_TestNames)
Set MyLabel = Me.Controls.Add("Forms.Label.1")
With MyLabel
.Name = "lbl_Test" & i
.Font.Bold = True
.Font.Size = 10
.Font.Name = "Tahoma"
.Height = 36
.Top = TopOffset + (i * LabelOffset)
.Left = 2 + MaxWidth
.Caption = ""
.Visible = True
.Width = 20
End With
Next
'add checkbox for every FMT test and set them TRUE and invisible
For i = 1 To UBound(FMT_TestNames)
Set MyCheckbox = Me.Controls.Add("Forms.checkbox.1")
With MyCheckbox
.Name = "Checkbox" & i
.Width = 14
.Height = 10
.Top = TopOffset + (i * LabelOffset)
.Left = 45 + MaxWidth
.Visible = False
.value = True
End With
Next
'add eventcode for every checkbox
Set TempForm =
ThisWorkbook.VBProject.VBComponents.Item("frmprofilegraph")
With TempForm.CodeModule
x = .CountOfLines
For i = 1 To UBound(FMT_TestNames)
Debug.Print "checkbox" & i
On Error GoTo Errorhandler
.InsertLines x + 1, "Private Sub Checkbox" & i &
"_Change()"
.InsertLines x + 2, " if me.value=true then"
.InsertLines x + 3, " ShowFail"
.InsertLines x + 4, " else"
.InsertLines x + 5, " HideFail"
.InsertLines x + 6, " endif"
.InsertLines x + 7, "End Sub"
Next
End With
End If
With Me
.Image1.Picture = LoadPicture(ThisWorkbook.Path & "\" &
MyChartName & ".gif")
.Image1.PictureSizeMode = fmPictureSizeModeStretch
.tbSerieNumber.SetFocus
.lblStatus.Caption = ""
End With
StartUp = False
GraphData = True
Exit Sub
Errorhandler:
Debug.Print Err.Description
Debug.Print Err.Number
Resume Next
End Sub
-------------------
Here's part of the code generated by the "add eventcode for every
checkbox" loop:
Private Sub Checkbox2_Change()
If Me.value = True Then
ShowFail
Else
HideFail
End If
End Sub
Private Sub Checkbox1_Change()
If Me.value = True Then
ShowFail
Else
HideFail
End If
End Sub
...... until
Private Sub Checkbox21_Change()
If Me.value = True Then
ShowFail
Else
HideFail
End If
End Sub
------
Anyone who can help me solve this problem?
I realy want it up and running, its also a chalenge.
Thanks in advance,
Regards,
Ludo
I have a userform (frmProfileGraph) where I add some controls
programatically (see example:
http://spreadsheetpage.com/index.php/site/tip/creating_a_userform_programmatically/)
, the number of controls is depending on the number of executed test,
so this one is variable.
Thats why i want to add them programmatically and not fixed.
The controls i add are labels and checkboxes.
So far it works fine, but i want to add programatically some code for
the checkboxes when clicking on a checkbox (change event) that
depending on the TRUE / FALSE condition a routine is executed. And
here it fails.
The code for the checkboxes is added to the codemodule (see below the
Userform initialize code) , but i get an Automation error as follow:
Run-time error '-214717848(80010108)':
Automation error
The object invoked has disconnected from its clients.
I hoped to get some information by adding a errorhandler in the code,
but this isn't invoked. I get the Automation error before the
errorhandler is executed.
Here's my code:
Private Sub UserForm_Initialize()
Dim MyLabel As MSForms.Label
Dim MyCheckbox As MSForms.CheckBox
Dim i As Integer
Dim MaxWidth As Long 'max width for the labels
Const LabelOffset = 16 'offset between the different FMT test
labels
Const TopOffset = 140
'if a graph has been made before, clear the combobox
If GraphMade = True Then
Me.lbFMT_LogList.Clear
End If
If StartUp = True Then
'add labels with the FMT Test name
For i = 1 To UBound(FMT_TestNames)
Set MyLabel = Me.Controls.Add("Forms.Label.1")
With MyLabel
.Name = "lbl_TestName" & i
.Font.Bold = True
.Font.Size = 10
.Font.Name = "Tahoma"
.WordWrap = False
.Width = 102
.Height = 36
.Top = TopOffset + (i * LabelOffset)
.Left = 2
.Caption = FMT_TestNames(i)
.Visible = True
If .Width > MaxWidth Then
MaxWidth = .Width
End If
End With
Next
'add labels to place failure count in it
For i = 1 To UBound(FMT_TestNames)
Set MyLabel = Me.Controls.Add("Forms.Label.1")
With MyLabel
.Name = "lbl_Test" & i
.Font.Bold = True
.Font.Size = 10
.Font.Name = "Tahoma"
.Height = 36
.Top = TopOffset + (i * LabelOffset)
.Left = 2 + MaxWidth
.Caption = ""
.Visible = True
.Width = 20
End With
Next
'add checkbox for every FMT test and set them TRUE and invisible
For i = 1 To UBound(FMT_TestNames)
Set MyCheckbox = Me.Controls.Add("Forms.checkbox.1")
With MyCheckbox
.Name = "Checkbox" & i
.Width = 14
.Height = 10
.Top = TopOffset + (i * LabelOffset)
.Left = 45 + MaxWidth
.Visible = False
.value = True
End With
Next
'add eventcode for every checkbox
Set TempForm =
ThisWorkbook.VBProject.VBComponents.Item("frmprofilegraph")
With TempForm.CodeModule
x = .CountOfLines
For i = 1 To UBound(FMT_TestNames)
Debug.Print "checkbox" & i
On Error GoTo Errorhandler
.InsertLines x + 1, "Private Sub Checkbox" & i &
"_Change()"
.InsertLines x + 2, " if me.value=true then"
.InsertLines x + 3, " ShowFail"
.InsertLines x + 4, " else"
.InsertLines x + 5, " HideFail"
.InsertLines x + 6, " endif"
.InsertLines x + 7, "End Sub"
Next
End With
End If
With Me
.Image1.Picture = LoadPicture(ThisWorkbook.Path & "\" &
MyChartName & ".gif")
.Image1.PictureSizeMode = fmPictureSizeModeStretch
.tbSerieNumber.SetFocus
.lblStatus.Caption = ""
End With
StartUp = False
GraphData = True
Exit Sub
Errorhandler:
Debug.Print Err.Description
Debug.Print Err.Number
Resume Next
End Sub
-------------------
Here's part of the code generated by the "add eventcode for every
checkbox" loop:
Private Sub Checkbox2_Change()
If Me.value = True Then
ShowFail
Else
HideFail
End If
End Sub
Private Sub Checkbox1_Change()
If Me.value = True Then
ShowFail
Else
HideFail
End If
End Sub
...... until
Private Sub Checkbox21_Change()
If Me.value = True Then
ShowFail
Else
HideFail
End If
End Sub
------
Anyone who can help me solve this problem?
I realy want it up and running, its also a chalenge.
Thanks in advance,
Regards,
Ludo