Windows XP / Excel 2010 Pro.
I have a snippet of code that I use to format form controls based on user input. This code seems to run perfectly fine in one workbook, and not at all in another. The code in both workbooks is the exact same and both are located in a public module. The only possible difference I can see is that in the workbook that has no issues the control being passed is located in a userform; in the non-functioning workbook the same control is located on a spreadsheet.
If the FormatControl function is passed controls that are in a user form the function works just fine in both workbooks; whether I pass it a TextBox, ComboBox, ListBox, or any other MSForms library control. However, when I pass a MSForms library control that has been placed on a spreadsheet to the FormatControl function it crashes with a TypeMismatch error.
CheckCBOText is a special function for checking to make sure that users selected a value from a combo box control’s drop-down list instead of typing in a new value. The control is then forwarded to the FormatControl function for formatting. This function receives a combo box control with no problems, whether the control is located on a user form or a worksheet. However, if the combo box is located on a worksheet, I get a mismatch error when the control is forwarded to the FormatControl function for formatting.
Since the MSForms.Combox class inherits the MSForms.Control class, I cannot see why this error occurs, regardless of the location of the control. Can anyone provide any insight on the matter?
-Joshua
Well, You can try banging your head against the wall, but you just end up with lost-time injuries and damaged equipment. [M. Passman]
I have a snippet of code that I use to format form controls based on user input. This code seems to run perfectly fine in one workbook, and not at all in another. The code in both workbooks is the exact same and both are located in a public module. The only possible difference I can see is that in the workbook that has no issues the control being passed is located in a userform; in the non-functioning workbook the same control is located on a spreadsheet.
If the FormatControl function is passed controls that are in a user form the function works just fine in both workbooks; whether I pass it a TextBox, ComboBox, ListBox, or any other MSForms library control. However, when I pass a MSForms library control that has been placed on a spreadsheet to the FormatControl function it crashes with a TypeMismatch error.
CheckCBOText is a special function for checking to make sure that users selected a value from a combo box control’s drop-down list instead of typing in a new value. The control is then forwarded to the FormatControl function for formatting. This function receives a combo box control with no problems, whether the control is located on a user form or a worksheet. However, if the combo box is located on a worksheet, I get a mismatch error when the control is forwarded to the FormatControl function for formatting.
Since the MSForms.Combox class inherits the MSForms.Control class, I cannot see why this error occurs, regardless of the location of the control. Can anyone provide any insight on the matter?
Code:
Public Enum enmControlFormat
enmNoFormat = 1
enmHasFocus = 2
enmBadInfo = 3
enmInfoRequested = 4
enmInfoRequired = 5
End Enum
Public Function CheckCBOText(cboBox As MSForms.ComboBox, Optional NullStringAllowed As Boolean = True) As Boolean
'Checks the text in a combo box to make sure it is valid. Changes backcolor of the control accordingly.
If cboBox.ListIndex < 0 And Trim(cboBox.Text) <> vbNullString Then
[highlight]Call FormatControl(cboBox, enmBadInfo) [b][COLOR=red] Error Line[/color][/b][/highlight]
CheckCBOText = False
Else
Call FormatControl(cboBox, enmNoFormat)
CheckCBOText = True
End If
If NullStringAllowed = False And Trim(cboBox.Text) = vbNullString Then
Call FormatControl(cboBox, enmInfoRequired)
CheckCBOText = False
End If
End Function
Public Sub FormatControl(ctrControl As MSForms.Control, Optional FormatOption As enmControlFormat = enmNoFormat, Optional DefaultBackColor As Long = lngcFontColorWhite, Optional DefaultFocusColor As Long = lngcFontColorLightBlue)
On Error Resume Next
Select Case FormatOption
Case enmBadInfo
ctrControl.ForeColor = lngcFontColorDarkRed
ctrControl.BackColor = lngcFontColorLightRed
ctrControl.BorderStyle = fmBorderStyleSingle
ctrControl.BorderColor = lngcBorderColorBad
Case enmHasFocus
ctrControl.ForeColor = lngcFontColorBlack
ctrControl.BackColor = DefaultFocusColor
ctrControl.BorderColor = lngcBorderColorBad 'Will not appear with no border
ctrControl.BorderStyle = fmBorderStyleNone
ctrControl.SpecialEffect = fmSpecialEffectSunken
Case enmInfoRequested
ctrControl.ForeColor = lngcFontColorBlack
ctrControl.BackColor = lngcInfoRequiredColor
ctrControl.BorderColor = lngcBorderColorBad 'Will not appear with no border
ctrControl.BorderStyle = fmBorderStyleNone
ctrControl.SpecialEffect = fmSpecialEffectSunken
Case enmInfoRequired
ctrControl.BorderStyle = fmBorderStyleSingle
ctrControl.BorderColor = lngcBorderColorBad
ctrControl.BackColor = lngcInfoRequiredColor
Case enmNoFormat
ctrControl.ForeColor = lngcFontColorBlack
ctrControl.BackColor = DefaultBackColor
ctrControl.BorderColor = lngcBorderColorBad 'Will not appear with no border
ctrControl.BorderStyle = fmBorderStyleNone
ctrControl.SpecialEffect = fmSpecialEffectSunken
End Select
On Error GoTo 0
End Sub
-Joshua
Well, You can try banging your head against the wall, but you just end up with lost-time injuries and damaged equipment. [M. Passman]