Class property error VBA - vba

I am getting runtine error 91: Object vriable or With block variable not set whne I run a simple class manipulation code.
Here are my classes
cTask:
Private pMile As cMile
Public Property Get Mile() As cMile
Set Mile = pMile
End Property
Public Property Set Mile(Value As cMile)
Set pMile = Value
End Property
Private Sub Class_Initializer()
Set Me.Mile = New cMile
End Sub
cMile:
Private pstatus As String
Public Property Get status() As String
status = ppstatus
End Property
Public Property Let status(Value As String)
pstatus = Value
End Property
And the sub:
Sub testt()
Dim ct As New cTasks
ct.Mile.status = "compl"
Debug.Print ct.Mile, ct.Mile.status
End Sub
The code goes from the sub to the get property in cTask. When about to execute the "End Propety" line the error pops up.
I guess something must be wrong with my classes but I don not know what. I have just recently started using classes. Any ideas?
Thank you

You have a typo:
Class_Initializer()
should be
Class_Initialize()
This prevents ct's Mile from being created so accessing it raises the error you see.
ppstatus is also spelt incorrectly.

Related

Create a class modules with parent and child classes in VBA

I am a novice user of class module.
I don't understand the concept of a class module well.
I want to configure a class module similar to the basic objects of Excel like Worksheet or Cells or ETC..
So I want to control it by creating a parent object and creating its child objects.
Child Class - Defect
Option Explicit
Private pDefectSymptom As String
Private pDefectLevel As Integer
Property Get DefectSymptom() As String
DefectSymptom = pDefectSymptom
End Property
Property Let DefectSymptom(ByVal vDefectSymptom As String)
pDefectSymptom = vDefectSymptom
End Property
Property Get DefectLevel() As Integer
DefectLevel = pDefectLevel
End Property
Property Let DefectLevel(ByVal vDefectLevel As Integer)
pDefectLevel = vDefectLevel
End Property
Function Delete()
'???
End Function
Property Get Parent() As Object
'???
End Property
Parent Class - Defects
Private Defects As New Collection
Function Add(DefectSymptom As String, Optional DefectLevel As Integer) As Defect
Dim NewDefect As Defect
Set NewDefect = New Defect
NewDefect.DefectSymptom = DefectSymptom
NewDefect.DefectLevel = DefectLevel
Defects.Add NewDefect
'Add = NewDefect 'Error! Like the open command of workbook, I want to return an object or just command
End Function
Property Get Count() As Long
Count = Defects.Count
End Property
Property Get Item(Index As Long) As Defect
Item = Defects(Index) 'Error! I don't know what raise Error.
End Property
My question are.
How to add Command like Open Command of workbook. Return or just command.
Why Raise Error Item Property? how to fix that?
Hiding Private variable. Because office office objects seem to be hidden.
enter image description here
If you have time, please help with Delete command and Parent command.
How to add Command like Open Command of workbook. Return or just
command.
You just need to return the newly created object in the function. Keep in mind since we're dealing with objects, we need to Set the object's reference.
Public Function Add(DefectSymptom As String, Optional DefectLevel As Integer) As Defect
Dim NewDefect As Defect
Set NewDefect = New Defect
NewDefect.DefectSymptom = DefectSymptom
NewDefect.DefectLevel = DefectLevel
Defects.Add NewDefect
Set Add = NewDefect '<- here
End Function
Why Raise Error Item Property? how to fix that?
Same as the above, you need to Set the object's reference.
Property Get Item(Index As Long) As Defect
Set Item = Defects(Index)
End Property
To delete, simply supply the index to the function. However, this method must reside where the collection is (parent) since a Defect object cannot delete itself.
Function Delete(ByVal Index As Long)
Defects.Remove Index
End Function
Lastly, to hold a reference to the parent, each child must hold a reference to it in a private variable. Then you need to set the parent when creating a new item using the keyword Me.
So in the Defect class, create a private field.
Private mParent As Defects
Property Set Parent(ByVal objDefects As Defects)
Set mParent = objDefects
End Property
Property Get Parent() As Defects
Set Parent = mParent
End Property
With this done, amend the Add() method to store the reference.
Public Function Add(DefectSymptom As String, Optional DefectLevel As Integer) As Defect
Dim NewDefect As Defect
Set NewDefect = New Defect
NewDefect.DefectSymptom = DefectSymptom
NewDefect.DefectLevel = DefectLevel
Set NewDefect.Parent = Me '<- here
Defects.Add NewDefect
Set Add = NewDefect '<- here
End Function
Not sure this is a good idea though. I tend to avoid circular references altogether, since a child can hold the parent in memory by holding a reference to it. You will need to make sure to clear the reference to the Parent when deleting the item.
Lastly, you should avoid creating the Defects collection like this. Instead, you should make use of the class constructor and destructor.
This method is called automatically when a new class is created:
Private Sub Class_Initialize()
Set Defects = New VBA.Collection
End Sub
This method is called just before the class is destroyed from memory.
Private Sub Class_Terminate()
Set Defects = Nothing
End Sub

Add Collection as an item within another collection - Class - Excel VBA

I'm currently trying to build a collection of items wherein a collection might contain an another collection as an Item within.
I've set two collections and created a class module for each:
col1 - (linked to Class1); and col2 - (linked to Class2)
Below are my Class Modules:
Class1:
Option Explicit
Private pTestC1A As String
Private pTestC1B As Collection
Public Property Let TestC1A(Value As String)
pTestC1A = Value
End Property
Public Property Get TestC1A() As String
TestC1A = pTestC1A
End Property
Property Set TestC1B(col2 As Collection)
Set pTestC1B = col2
End Property
Property Get TestC1BElements(v As Integer) As String
TestC1B = pTestC1B(v)
End Property
Class2:
Option Explicit
Private pTestC2A As String
Public Property Let TestC2A(Value As String)
pTestC2A = Value
End Property
Public Property Get TestC2A() As String
TestC2A = pTestC2A
End Property
Below is my Module code
Sub Test()
Set col1 = New Collection
Set col2 = New Collection
Set cV = New Class1
cV.TestC1A = "First Collection"
Set aV = New Class2
aV.TestC2A = "Second Collection"
sKey1 = CStr(aV.TestC2A)
col2.Add aV, sKey1
Set cV.TestC1B = col2
sKey2 = CStr(cV.TestC1A)
col1.Add cV, sKey2
If Err.Number = 457 Then
MsgBox "Error Occured"
ElseIf Err.Number <> 0 Then Stop
End If
Err.Clear
Msgbox col1(1).TestC1A ' works fine
Msgbox col2(1).TestC2A ' works file
MsgBox col1(1).TestC1B(1).TestC2A ' does not work - 450 run-time error
End Sub
As per the above code, I'm successfully able to get the values of the items if I reference each collection respectively, however I'm getting a "Wrong number of arguments or invalid property assignment" run-time error if I try to get the item value in a nested fashion.
It would be appreciated if someone can help point out where I'm going wrong, and perhaps shed some light on the way the class module handles the Property Set & Get parameters for a collection.
You are missing a Get TestC1B property in your Class1 class module:
Property Get TestC1B() As Collection
Set TestC1B = pTestC1B
End Property
Once that is present you will be able to make calls to col1(1).TestC1B(1) and access it's .TestC2A property
Background
You did the right thing by using private variables in your classes and using properties to give read/write access to your private variables. You'll get a lot more control this way.
Property Get gives read access to that property (and broadly speaking, the underlying private variable). For example you can use Range.Address to return (read) the address of a range object.
Property Let and Set give write access to the property. Use Set for objects. For example Range.Value = 1 will write the new value to the range.
Consider, Range.Address = $A$1. Since there is no Property Set for the address of a range, this will not change the address of the range. It will consider the Range.Address part a Get call and evaluate something like $A$1 = $A$1 returning TRUE in this example

VBA pass parent class to child class

I found a great post on SO that seems to be exactly what I want: Is it possible to access a parent property from a child that is in a collection? However my adaptation of it is giving me Object doesn't support this property or method.
My code which now works thanks to Mat's Mug and Tomalak:
Parent Class - clsComputer
Option Explicit
Private pCD As clsCD
''''''''''''''''''''''''''''''
' CD property
''''''''''''''''''''''''''''''
Public Property Get CD() As clsCD
If pCD Is Nothing Then
Set pCD = New clsCD
'Per Mat's Mug post, drop the parenthesis
pCD.Initialze Me
End If
Set CD = pCD
End Property
Public Property Set CD(value As clsCD)
pCD = value
End Property
Child class - clsCD
Option Explicit
Private pParent As clsComputer
'''''''''''''''''''''''''''''
' Status property - READ ONLY
'''''''''''''''''''''''''''''
Public Property Get Status(Optional strHost As String) As String
Dim strResult As String
If strHost = "" Then strHost = Me.Parent.HostName
strResult = RunCMD("cmd /c ""winrs -r:" & strHost & _
" reg query hklm\system\currentcontrolset\services\cdrom /v start""")
If InStr(1, strResult, "0x4", vbTextCompare) Then
Status = "Disabled"
Else
Status = "Enabled"
End If
End Property
'''''''''''''''''''''''''
' Parent property
'''''''''''''''''''''''''
Public Property Get Parent() As clsComputer
Set Parent = pParent
End Property
'Because as Tomalak points out, you use Set with Objects.
Public Property Set Parent(Obj As clsComputer)
Set pParent = Obj
End Property
'''''''''''''''''''''''''
' Initialize Method
'''''''''''''''''''''''''
Public Sub Initialize(Obj As clsComputer)
Set Me.Parent = Obj
End Sub
Code Module - Module1
Sub test()
Dim oPC As clsComputer
Set oPC = New clsComputer
Debug.Print "CD Status: " & oPC.CD.Status
End Sub
If I test Me, it is an object (eg, If IsObject(Me) Then Stop evaluates true), and Intellisense shows all the properties and methods in clsComputer when I type Me. The Locals windows shows Me as a clsComputer object. Everything I know to check says Me is a clsComputer object, so what am I doing wrong?
Classic.
pCD.Initialize (Me) 'Error occurs on this line when using F8
Drop the parentheses.
pCD.Initialize Me
Done.
Parentheses around a parameter force it to be evaluated and passed ByVal (regardless of what the procedure's signature says) - and since you probably haven't defined a default property for clsComputer then the evaluation blows up and the runtime doesn't even get to the Initialize method.
That said, there's nothing wrong with passing object reference by value. In fact, that's what C# and VB.NET do by default - consider passing any parameter ByVal.
Public Property Set Parent(ByRef Obj As clsComputer)
Set pParent = Obj
End Property
I'm not by my PC so just coding blind
Try this for your clsComputer class
Option Explicit
Private pCD As clsCD
''''''''''''''''''''''''''''''
' CD property
''''''''''''''''''''''''''''''
Public Property Get CD() As clsCD
Set CD = pCD
End Property
Public Property Set CD(value As clsCD)
pCD = value
End Property
Sub Class_Initialize()
Set pCD = New clsCD
pCD.Initialize(Me)
End Property

VBA global class variable

My obstacle is trying to get multiple subs to recognize class variables. When I try to declare them globally, I get a compile error: "Invalid outside procedure". Then, when I run a public function or sub to declare the variables, they remain undefined in the other subs. I want multiple subs to recognize the variables because their values are supposed to be altered via UserForm, and then utilized in a different sub.
If it could work in this manner, great, but I understand that my design could fundamentally be flawed. Please advise!
This is my Class definition, inserted as a Class module named "cRSM":
Option Explicit
Private pName As String
Private pDesiredGrowth As Double
'Name of RSM
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(Value As String)
pName = Value
End Property
'Growth property
Public Property Get DesiredGrowth() As Double
DesiredGrowth = pDesiredGrowth
End Property
Public Property Let DesiredGrowth(Value As Double)
If Value > 0 And Value < 1 Then
pDesiredGrowth = Value
End If
End Property
This is invalid procedure error (which I put in the Global Declarations section):
'Bedoya
Dim Bedoya As cRSM
Set Bedoya = New cRSM
Bedoya.Name = "Bedoya"
And this is the "variable not defined error" (within a private sub):
Private Sub Add_Click()
**Bedoya.DesiredGrowth** = Txt2.Value
Thank you for your time
In a standard module (I name mine MGlobals), put
Public Bedoya As cRSM
Then in another standard module (I name mine MOpenClose), put
Sub Initialize()
If Not Bedoya Is Nothing Then
Set Bedoya = New cRSM
End If
End Sub
Any default properties you want set should be set in the Class_Initialize procedure. In any procedure that you want to use Bedoya, use
Initialize
and it will instantiate the global variable if necessary. The only difference between this and the New keyword is that you can't accidentally instantiate the variable with this method. You either call Initialize or you don't. A lot of VBA developers use New, but almost never do for that reason.
If I understood well You want a global object.
You can put the declaration in module like
public Bedoya As cRSM
then you have create the object ... you can use a global event inside the Workbook like
Private Sub Workbook_Open()
Set Bedoya = New cRSM
Bedoya.initialize("Bedoya") 'a method to initialize private variables
End Sub
Now you can use the global object. You have to restart the excel file or run this method manually.
Is not good style to use global variables, but sometimes is the more easy to do :P
What you want to do nowadays is done using singleton Software Pattern, but this is for other day hehehe

Referencing the Set Property from the Get Property

I came across this bit of vba code posted in another SO question. Is there any significance in referencing the Set property in the Get Property of a class?
Private WithEvents mctlEventButton As MSForms.CommandButton
Public Property Set EventButton(ctlButton As MSForms.CommandButton)
Set mctlEventButton = ctlButton
End Property
Why do this? ...
Public Property Get EventButton() As MSForms.CommandButton
Set EventButton = mctlEventButton
End Property
The code was showing how to use collections to iterate through a group of controls. The question wasnt really about that part of the code, so it wasnt addressed in the question. Using the example from the question, I ran into an issue with the bit I posted here because the Get Property was using the Set property. So when would that be useful?
Here is the link to the SO Question: object array or collection in VBA Excel
Public Property Get EventButton() As MSForms.CommandButton
Set EventButton = mctlEventButton
End Property
In the code above, Set EventButton = mctlEventButton won't call the following (try stepping through the code & you will see that it doesn't step into Set)
Public Property Set EventButton(ctlButton As MSForms.CommandButton)
Set mctlEventButton = ctlButton
End Property
On the other hand, this of it as a statement that is used to return a value.
Effectively, think of Get as
Public function EventButton() As MSForms.CommandButton
Set EventButton = mctlEventButton 'returning the value from the function
End Property
wrapped in form of a property for developers to do get/set.