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 6005765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:26:37+00:00 2026-05-23T01:26:37+00:00

Please would someone who understands VBA Arrays (Access 2003) help me with the following

  • 0

Please would someone who understands VBA Arrays (Access 2003) help me with the following code.

The idea is that ClassA holds a dynamic array of ClassB instances. The dynamic array starts empty. As callers call ClassA.NewB() then a new instance of ClassB is created, added to the array, and returned to the caller.
The problem is that I can’t return the new instance of ClassB to the caller, but get “Runtime error 91: Object variable or With block variable not set”

Also, a little WTF occurs where UBound() fails but wrapping the exact same call in another function works!?!? (Hence MyUbound() )

I’m from a C++ background and this VBA stuff is all a bit strange to me!

Thanks for any help!

Main code:

Dim a As clsClassA
Dim b As clsClassB

Set a = New clsClassA
a.Init
Set b = a.NewB(0)

clsClassA:

Option Compare Database

Private a() As clsClassB

Public Sub Init()
    Erase a
End Sub

Public Function NewB(i As Integer) As Variant
    'If (UBound(a, 1) < i) Then ' FAILS: Runtime error 9: Subscript out of range
    If (MyUBound(a) < i) Then ' WORKS: Returns -1
        ReDim Preserve a(0 To i)
    End If
    NewB = a(i) ' FAILS: Runtime error 91: Object variable or With block variable not set
End Function

Private Function MyUBound(a As Variant) As Long
    MyUBound = UBound(a, 1)
End Function

clsClassB:

Option Compare Database
' This is just a stub class for demonstration purposes
Public data As Integer
  • 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-05-23T01:26:38+00:00Added an answer on May 23, 2026 at 1:26 am

    Try this:

    Public Function NewB(i As Integer) As Variant
        'If (UBound(a, 1) < i) Then ' FAILS: Runtime error 9: Subscript out of range
        If (MyUBound(a) < i) Then ' WORKS: Returns -1
            ReDim Preserve a(0 To i)
        End If
    
        Set a(i) = New clsClassB
    
        Set NewB = a(i)
    End Function
    

    You need to set a(i) to a new instance of the class (or it will simply be null), you also need to use Set as you’re working with an object…

    I’d perhaps also suggest changing the return type of NewB to clsClassB rather than Variant.

    You could also do

    Public Sub Init()
        ReDim a(0 To 0)
        Set a(0) = New Class2
    End Sub
    

    to remove the need for the special UBound function.

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

Sidebar

Related Questions

would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
I would like to access a MySQL database using PHP. Can someone please explain
Please help this would be my last problem in dealing with access database with
I would appreciate if there is someone who can help me solve this problem,
Could someone please explain when would I want to use delegation instead of inheritance?
Can someone please explain me why would a UDP Server not need to use
Can someone please shed some light on how I would get this to work:
Would you please explain why the following shell command wouldn't work: sh-3.1$ echo $MYPATH
Would you please help me getting back the typename of the original object put
Would someone please be able to explain to me how to properly install JavaMail

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.