Object doesn't support this property in class module

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Object doesn't support this property in class module

Post by YasserKhalil »

Hello everyone
I have a userform with some checkboxes
In standard module I have this code

Code: Select all

Dim xCollection As New Collection
Public Sub ClsChk_Init()
'Dim xSht As Worksheet
Dim xObj As Object
Dim xChk As ClsChk
   'Set xSht = ActiveSheet
   Set xCollection = Nothing
    For Each xObj In UserForm1.Controls
        If xObj.Name Like "CheckBox**" Then
            Set xChk = New ClsChk
            Set xChk.Chk = CallByName(UserForm1, xObj.Name, VbGet)
            xCollection.Add xChk
        End If
    Next
    Set xChk = Nothing
    UserForm1.Show
End Sub
In class module named "ClsChk" I have this code

Code: Select all

Public WithEvents Chk As MSForms.CheckBox
Private Sub Chk_Click()
Call SelOneCheckBox(Chk)
MsgBox "OK"
End Sub

Private Sub Chk_Change()
Dim ctl As MSForms.Control
 If Chk.Value = True Then
 For Each ctl In UserForm1.Controls
 If TypeName(ctl) = "CheckBox" And Chk.Name <> ctl.Name Then
  ctl.Vaue = False
 End If
 Next ctl
 End If
End Sub

Sub SelOneCheckBox(Target As Object)
Dim xObj As Object
Dim I As String
Dim n As Integer
If Target.Object.Value = True Then
 
    I = Right(Target.Name, Len(Target.Name) - 8)
    For n = 1 To ActiveSheet.OLEObjects.Count
      If n <> Int(I) Then
        Set xObj = ActiveSheet.OLEObjects.Item(n)
        xObj.Object.Value = False
        xObj.Object.Enabled = False
      End If
    Next
Else
    I = Right(Target.Name, Len(Target.Name) - 8)
    For n = 1 To ActiveSheet.OLEObjects.Count
      If n <> Int(I) Then
        Set xObj = ActiveSheet.OLEObjects.Item(n)
        xObj.Object.Enabled = True
      End If
    Next
End If
End Sub
At this line ` ctl.Vaue = False`, I encountered an error `Object doesn't support this property ..`
What I am trying to achive through the class module is the check only one checkbox, in other words if there is a checked checkbok and I checked another one then to remove all the checks from all other checkboxes.

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Object doesn't support this property in class module

Post by HansV »

Change Vaue to Value :grin:
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Object doesn't support this property in class module

Post by HansV »

By the way, why not use radio buttons? They behave the way you want automatically: selecting one radio button in a group automatically deselects all others in the same group.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Object doesn't support this property in class module

Post by YasserKhalil »

Thanks a lot, my tutor. I didn't notice the missing letter l at all. Thank you very much.

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Object doesn't support this property in class module

Post by YasserKhalil »

At this line

Code: Select all

For Each ctl In UserForm1.Controls
the userform1 is referred to .. how can I deal with any userform with the same principle? I mean is there a keyword that is used when dealing with class modules?

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Object doesn't support this property in class module

Post by HansV »

Add the following declaration at the top of the class module:

Code: Select all

Public MyForm As Object
and modify Chk-Change to refer to MyForm:

Code: Select all

Private Sub Chk_Change()
    Dim ctl As MSForms.Control
    If Chk.Value = True Then
        For Each ctl In MyForm.Controls
            If TypeName(ctl) = "CheckBox" And Chk.Name <> ctl.Name Then
                ctl.Value = False
            End If
        Next ctl
    End If
End Sub
In the standard module, modify ClsChk_Init to set the MyForm property of the class module:

Code: Select all

Public Sub ClsChk_Init()
    Dim xObj As Object
    Dim xChk As ClsChk
    Set xCollection = Nothing
    For Each xObj In UserForm1.Controls
        If xObj.Name Like "CheckBox**" Then
            Set xChk = New ClsChk
            Set xChk.MyForm = UserForm1
            Set xChk.Chk = CallByName(UserForm1, xObj.Name, VbGet)
            xCollection.Add xChk
        End If
    Next
    Set xChk = Nothing
    UserForm1.Show
End Sub
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Object doesn't support this property in class module

Post by YasserKhalil »

Awesome. Thank you very much for your great support.