PRB: GPF in VBA2.DLL Accessing Collection in Local Class

Last reviewed: June 19, 1997
Article ID: Q170357
The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions, 16-bit only, for Windows, version 4.0

SYMPTOMS

With the 16-bit version of Microsoft Visual Basic 4.0, if you attempt to access a collection of a class, you may receive one of the following errors.

When running in Microsoft Windows 3.11 or Microsoft Windows For Workgroups 3.11, the error will say:

   "VB caused a General Protection Fault in module VBA2.DLL"

   -or-

   "Run-time Error 9:
   Subscript Out of Range"

When running in Microsoft Windows 95, the dialog box will report:

   "An error has occurred in your program"

After clicking the "Ignore" button several times, another dialog box will report:

   "Run time error '5': Invalid procedure call"

When running in Microsoft Windows NT:

   "An error has occurred in your application"

After clicking "Ignore" twice, you will see:

   "Run time error '458'
   Variable uses an OLE type not supported in Visual Basic"

CAUSE

This error will occur under the following circumstances:

  • The Class module is in the same project as the code that accesses the collection.

    -and-

  • The Public property of the class module is True.

    -and-

  • The collection is not explicitly instantiated before you access it. In other words, the collection is implicitly instantiated with the New keyword when the collection is dimensioned.

RESOLUTION

To work around this problem, explicitly instantiate the collection. For example, if you are receiving one of the errors described above with the following code:

   Public MyCollection As New Collection

   Private Sub Class_Initialize()
      MyCollection.Add "Hello"
   End Sub

Change the code to look like the following:

   Public MyCollection As Collection

   Private Sub Class_Initialize()
      Set MyCollection = New Collection
      MyCollection.Add "Hello"
   End Sub

STATUS

Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project.

  2. Click Class Module on the Insert menu to add a class module to the project.

  3. Type the following code in the code window of the class module:

          Public MyCollection As New Collection
    

          Private Sub Class_Initialize()
    
             MyCollection.Add "Hello"
          End Sub
    
    

  4. With the class module active, press the F4 key to display the property sheet. Set the Public property to True.

  5. Click Module on the Insert menu to add a module to the project.

  6. Type the following code in the code window of the module:

          Sub Main()
             Dim obj As New Project1.Class1
             MsgBox obj.MyCollection.Item(1)
             Set obj = Nothing
          End Sub
    
    

  7. Click Options on the Tools menu. Select the Project tab, set the Startup Form to "Sub Main" and click OK.

  8. Press the F5 key to run the project. You will receive one of the errors described at the beginning of this article.

  9. To work around this problem, modify the code in the class module so that it resembles the following:

          Public MyCollection As Collection
    

          Private Sub Class_Initialize()
    
             Set MyCollection = New Collection
             MyCollection.Add "Hello"
          End Sub
    


Additional query words: gpf vb4 16bit 16 bit 0005:1E3A 0005:1B21 0005:2027
Keywords : kberrmsg PrgOther vb416 vb4win kbprb
Version : 4.0
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.