Using Forms, how to call sub classes from an eventclass

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

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật