Note that there are some explanatory texts on larger screens.

plurals
  1. POVBA Arrays - test for empty, create new, return element
    text
    copied!<p>Please would someone who understands VBA Arrays (Access 2003) help me with the following code.</p> <p>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"</p> <p>Also, a little WTF occurs where UBound() fails but wrapping the exact same call in another function works!?!? (Hence MyUbound() )</p> <p>I'm from a C++ background and this VBA stuff is all a bit strange to me!</p> <p>Thanks for any help!</p> <p>Main code:</p> <pre><code>Dim a As clsClassA Dim b As clsClassB Set a = New clsClassA a.Init Set b = a.NewB(0) </code></pre> <p>clsClassA:</p> <pre><code>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) &lt; i) Then ' FAILS: Runtime error 9: Subscript out of range If (MyUBound(a) &lt; 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 </code></pre> <p>clsClassB:</p> <pre><code>Option Compare Database ' This is just a stub class for demonstration purposes Public data As Integer </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload