1

I want to create the following structure: enter image description here

I have created the following code:

Userform with a commandbutton (used to activate the code)

Option Explicit

Public myCollection As Collection

Private Sub CommandButton1_Click()
    Dim subItem As SubClass
    Dim baseItem As BaseClass

    Set baseItem = New BaseClass
    Set subItem = New SubClass

    subItem.itemName = "Something"

    baseItem.addSubItem (subItem)    <---- ERROR ON THIS ROW!

    myCollection.Add (baseItem)
End Sub

Private Sub UserForm_Initialize()
    Set myCollection = New Collection
End Sub

BaseClass

Option Explicit

Private subClassCollection As Collection

Public Sub addSubItem(subItem As SubClass)
    Call subClassCollection.Add(subItem, Key:=subItem.itemName)
End Sub

Private Sub Class_Initialize()
    Set subClassCollection = New Collection
End Sub

SubClass

Option Explicit

Private sItemName As String

Property Get itemName() As String
    itemName = sItemName
End Property

Property Let itemName(name As String)
    sItemName = name
End Property

When I run this code I get the following error "Object doesn't support this property or method" on the highlighted row in the Userform.

I thought this example would be simple enough.. what am I missing?

RedSmolf
  • 355
  • 3
  • 11
  • 2
    `baseItem.addSubItem (subItem)` should be `baseItem.addSubItem subItem` (There is no way for `subItem` to be converted to something that could be used in an expression such as `(subItem)`.) – YowE3K Nov 18 '17 at 08:25
  • So when I write baseItem.addSubItem (subItem) I actually try to convert something? I am new to VBA/VB syntax. – RedSmolf Nov 18 '17 at 08:32
  • 1
    The same goes for the expression `(baseItem)`, so just use the object when adding to the collection, i.e. `myCollection.Add baseItem`. And you need to set `myCollection` to be a `New Collection` at some point. – YowE3K Nov 18 '17 at 08:32
  • Yes, the syntax for calling subroutines is `subname parm1, parm2`. So you were make the first parameter `(subItem)`, i.e. an expression involving `subItem`. – YowE3K Nov 18 '17 at 08:33
  • 1
    Sorry - I had missed your `Set myCollection = New Collection` in your form initialisation. (I had tested by just running the `CommandButton1_Click` within a normal module, and overlooked that the form would run the `UserForm_Initialize()` code before that.) So there's no problems with that part of the code - just with the two bracketed parameters. – YowE3K Nov 18 '17 at 08:41

1 Answers1

2

You should not put brackets around parameters when calling subroutines / methods. The brackets are forcing VBA to try and evaluate the expression (subItem) (for instance), which it can't do.

The following main code should run. (No guarantees that it is doing the correct thing, but it won't crash at least.)

Option Explicit

Public myCollection As Collection

Private Sub CommandButton1_Click()
    Dim subItem As SubClass
    Dim baseItem As BaseClass

    Set baseItem = New BaseClass
    Set subItem = New SubClass

    subItem.itemName = "Something"

    baseItem.addSubItem subItem

    myCollection.Add baseItem
End Sub

Private Sub UserForm_Initialize()
    Set myCollection = New Collection
End Sub

The syntax for invoking procedures, etc, is something like:

procedure parm1, parm2, parm3

or (the obsolete version)

Call procedure(parm1, parm2, parm3)

or (to use a Function)

returnValue = procedure(parm1, parm2, parm3)
YowE3K
  • 23,852
  • 7
  • 26
  • 40