I have several forms and several TextBoxes in each form. To isolate the code I use a class CCtrlTextBoxEvent
enabling events: Public WithEvents txtbox As MSForms.TextBox and i get callbacks on txtbox_Change().
‘Each Form implements IForm which basically has one declaration:
Public Function ChangeStatus(bSet As Boolean, iBit As Integer) As Boolean
'My Form has following code at the start:
Implements IForms
'If changes has been done, save it for enabling buttons
Private bDirty As Boolean
Private txtBoxes() As ICtrlEvent
Private glBWFieldsChanged As Integer
'Callback function to enable/disable controls in form
'Common function to set or clear a bit dependent on the bSet condition
Public Function IForms_ChangeStatus(bSet As Boolean, iBit As Integer) As Boolean
If bSet Then
Call SetBit(glBWFieldsChanged, iBit)
Else
Call ClearBit(glBWFieldsChanged, iBit)
End If
bDirty = glBWFieldsChanged > 0
EnableButtons
End Function
UserForm_Initialize() 'i set up this structure:
ReDim txtBoxes(bwiAcDoStart To bwiAcDoEnd)
'Textboxes has to start with 'TB' and comboboxes has to start with 'Cbo'
'Setup textbox Tb_AccDocDate
Set txtBoxes(bwiAcDoDate) = GetCtrlClass("Tb_AccDocDate")
Call txtBoxes(bwiAcDoDate).Init(Me, GetControl("Tb_AccDocDate"), bwiAcDoDate, "Date")
...
End Sub
Public Function GetCtrlClass(sCtrlName As String) As ICtrlEvent
... error handling
If Left(sCtrlName, 2) = "Tb" Then
Set GetCtrlClass = New CCtrlTextBoxEvent
ElseIf Left(sCtrlName, 3) = "Cbo" Then
Set GetCtrlClass = New CCtrlComboEvent
Else
MsgBox ("GetCtrlClass, unknown ctrl type '" & sCtrlName & "'")
End If
... error handling
End Function
-
—
new class
Class CCtrlTextBoxEvent:
Implements ICtrlEvent
Public WithEvents txtbox As MSForms.TextBox
Private usrForm As IForms
Private referenceString As String
Private bitwCtrl As Integer
Private ctrlType As ICtrlType
Public Sub ICtrlEvent_Init(ByRef uf As IForms, ByRef ctrl As Control, bitw As Integer, sCtrlType As String)
Set usrForm = uf
Set txtbox = ctrl
bitwCtrl = bitw
referenceString = ""
' Set ctrlType = GetCtrlTypeClass(sCtrlType)
' ctrlType.Init (ctrl)
End Sub
‘If New item is loaded, the text will be changed, this is the right entry
Public Sub ICtrlEvent_SetReference(refStr As String)
referenceString = refStr
End Sub
Private Sub txtbox_Change()
Call usrForm.ChangeStatus(txtbox.Value <> referenceString, bitwCtrl)
End Sub
So far everything works fine.
Give the control in as a parameter to ICtrlEvent_Init(…) and setup a callback
I get the callback and ChangeStatus get called and no problem
The text boxes in Forms should have different Types like: String, Date, Long, Currency…
What i wanted was to have sub classes for each type, testing on format and if it has valid content.
Implementing a ICtrlType interface and GetCtrlTypeClass to create sub classes CTypeStr, CTypeLong, CType Date and CTypeCurrency. implementing this interface.
First i included extra functions in CCtrlTextBoxEvent
Could probably been properties
Public Function ICtrlEvent_GetValue() As Variant
If Not ctrlType Is Nothing Then
ICtrlEvent_GetValue = ctrlType.GetValue
End If
End Function
Public Function ICtrlEvent_SetValue(ByVal vNewValue As Variant)
If Not ctrlType Is Nothing Then
ctrlType.SetValue (vNewValue)
End If
End Function
Public Function ICtrlEvent_IsValid() As Boolean
If Not ctrlType Is Nothing Then
ICtrlEvent_IsValid = ctrlType.IsValid
End If
End Function
In the CCtrlTextBoxEvent, function ICtrlEvent_Init(…)
I have comented out two lines of code, because its where my problem is
' Set ctrlType = GetCtrlTypeClass(sCtrlType)
' ctrlType.Init (ctrl)
Public Function GetCtrlTypeClass(sCtrlType As String)
...
If sCtrlType = "Combo" Then
Set GetCtrlTypeClass = New CTypeCombo
ElseIf sCtrlType = "Cur" Then
Set GetCtrlTypeClass = New CTypeCurrency
ElseIf sCtrlType = "Date" Then
Set GetCtrlTypeClass = New CTypeDate
ElseIf sCtrlType = "Long" Then
Set GetCtrlTypeClass = New CTypeLong
ElseIf sCtrlType = "Str" Then
Set GetCtrlTypeClass = New CTypeStr
Else
MsgBox ("GetCtrlTypeClass, unknown ctrl type '" & sCtrlType & "'")
End If
...
End Function
class CTypeDate :
Implements ICtrlType
Dim og_ctrl As MSForms.TextBox
Public Function ICtrlType_Init(ByRef cCtrl As Control) As Boolean
Set og_ctrl = cCtrl
End Function
Public Function ICtrlType_GetValue() As Variant
...
End Function
Public Function ICtrlType_SetValue(ByVal vNewValue As Variant)
...
End Function
Public Function ICtrlType_IsValid() As Boolean
...
End Function
You see its easy to find the control and set it in the class CCtrlTextBoxEvent
to do: Set txtbox = ctrl -> defined like Public WithEvents og_ctrl As MSForms.TextBox
but trying to call:
ctrlType.Init (ctrl) fails and get a error 424 at runtime.
I can se in documentation, but i dont know how to fix it :
You referred to an object property or method, but didn’t provide a valid object qualifier. Specify an object qualifier if you didn’t provide one. For example, although you can omit an object qualifier when referencing a form property from within the form’s own module, you must explicitly specify the qualifier when referencing the property from a standard module.
Can anyone help me
I tried to set a variable Public WithEvents txtbox As MSForms.TextBox
in a subclass using ctrlType.Init(txtbox) to following class starting with
Implements ICtrlType
Dim og_ctrl As MSForms.TextBox
Public Function ICtrlType_Init(ByRef cCtrl As Control) As Boolean
Set og_ctrl = cCtrl
End Function
Everything compiles, its during runtime I get the error 424
Jo-Helge Rorvik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.