You can't set a form control from a variable. What you can do is declare a variable, then set the form control to that variable and modify that variable's properties. With MSForms (presumably Access forms?) you can also declare form variables and then add them to forms at runtime.
So I see two problems with your code. The first is that since the TreeControl is an ActiveX variable, you need to declare the TreeView0
variable as a CustomControl
not a TreeView
. Then you need to set the form control to the variable. ie Set TreeView0 = Me.TreeViewTest
.
So the code would be:
Private Sub Form_Load()
Dim iml As ImageList
Dim TreeView0 As CustomControl
'Set TreeView0 = New TreeView -> not needed as you set the control to the variable
Set iml = New ImageList
Set TreeView0 = Me.TreeViewTest
'Do the rest of you initialisation here.
End Sub
If you want to reuse this code (why else would you want to set it from a variable?) then you simply change this to:
Private Sub Form_Load()
InitMyTree(Me.TreeViewTest)
InitMyTree(Me.TreeViewTest1)
End Sub
And then sinmply the following function (note you don't need to declare the variable within the function).
Private Sub InitMyTree(TreeView0 as CustomControl)
Dim iml As ImageList: Set iml = New ImageList
iml.ListImages.Add , "Open", LoadPicture("Folder\Open.bmp")
iml.ListImages.Add , "Closed", LoadPicture("Folder\Close.bmp")
'Finish initialisation here.
End Sub