Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:29:01+00:00 2026-06-10T16:29:01+00:00

I’ve searched for a bit, but I couldn’t find a similar enough question/answer. So

  • 0

I’ve searched for a bit, but I couldn’t find a similar enough question/answer. So here it goes:

I have a class object called Project. A Project can have multiple Scenarios assoicated with it.

I’ve created the class modules for each object. But I am having difficulty in, I believe, instantiating the Scenarios collection for a given Project.

Here are the class modules:

1) cProject:

Private pProjectID As Integer 
Private pName As String 
Private pDateCreated As String 
Private pScenarios As cScenarios

' PROPERTIES

Public Property Get ProjectID() As Integer
    ProjectID = pProjectID 
End Property
Public Property Let ProjectID(value As Integer)
    pProjectID = value 
End Property
Public Property Get name() As String
    name = pName 
End Property 
Public Property Let name(value As String)
    pName = value 
End Property 
Public Property Get Scenarios() As cScenarios
    Set Scenarios = pScenarios 
End Property
Public Property Set Scenarios(value As cScenarios)
    Set pScenarios = value 
End Property

2) cScenarios collection class module:

Private pScenarios As Collection

Private Sub Class_Initialize()
    Set pScenarios = New Collection
End Sub
Private Sub Class_Terminate()
    Set pScenarios = Nothing
End Sub
Public Function Item(index As Variant) As cScenario
    Set Item = pScenarios.Item(index)
End Function
Public Property Get Count() As Long
    Count = pScenarios.Count
End Property
Public Sub Add(obj As cScenario)
    pScenarios.Add obj
End Sub
Public Sub Remove(index As Variant)
    pScenarios.Remove index
End Sub

And finally (3) the Scenario class object:

Private pScenarioID As Integer
Private pName As String
Private pDateCreated As String
Private pParent As cProject

Public Property Get ScenarioID() As Integer
    ScenarioID = pScenarioID
End Property
Public Property Let ScenarioID(value As Integer)
    pScenarioID = value
End Property
Public Property Get name() As String
    name = pName
End Property
Public Property Let name(value As String)
    pName = value
End Property
Public Property Get parent() As cProject
    parent = pParent
End Property
Public Property Let parent(value As cProject)
    pParent = value
End Property

Here is a standard module:

Sub test1()

Dim cS As cScenarios
Dim s As cScenario

Set cS = New cScenarios

For i = 1 To 3
    Set s = New cScenario
    s.name = "s" & i
    cS.Add s
Next

Debug.Print cS.Item(3).name
Debug.Print cS.Count

End Sub

This works. All is good. For now. I am able to populate cS with multiple scenarios. However, if I reference the scenarios collection as a child object of the project (see below in test2() ), I get a “Run-time error ’91’: Object variable or With block variable not set” triggered on the cs.Add call.

Sub test2()

Dim p As cProject
Dim cS As cScenarios
Dim s As cScenario

Set p = New cProject
Set cS = p.Scenarios

For i = 1 To 3
    Set s = New cScenario
    s.name = "s" & i
    cS.Add s
Next

Debug.Print cS.Item(3).name
Debug.Print cS.Count

End Sub

What did I do wrong building my class modules and/or how do I fix it? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:29:02+00:00Added an answer on June 10, 2026 at 4:29 pm

    You are not initializing pScenarios in the cProject class before trying to access it with Add().
    You can fix that by adding an initializer to cProject:

    Private Sub Class_Initialize()
        Set pScenarios = New cScenarios
    End Sub
    

    This will guarantee that the cS instance will not be Nothing when you try to invoke Add on it inside test2.
    Another way (weaker IMO) would be to set p.Scenarios = new cScenarios after newing up p inside test2.
    Also, make sure that the property setter for cScenario.parent is Property Set instead of Property Let.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.