DOCUMENT:Q183164 30-MAR-1998 [vbwin] TITLE :BUG: For..Each Loop in ActiveX Component Causes Errors PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:5.0,97sp2,97sp3 OPER/SYS:WINDOWS KEYWORDS:vb5all VBKBAX kberrmsg ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0 - Microsoft Visual Studio versions 97sp2, 97sp3 --------------------------------------------------------------------- SYMPTOMS ======== When using an ActiveX DLL created in Visual Basic, you may receive one of the following errors if the ActiveX component contains a For..Each loop that iterates through a Visual Basic collection: Run-time error '48': Error in loading DLL -or- Run-time error '-2147319765 (8002802b)': Automation Error This problem occurs when a For..Each loop is executed in an ActiveX DLL. For..Each loops elsewhere do not exhibit this problem. CAUSE ===== This problem typically occurs after calling the DLL at least twice. The first call into the DLL does not execute code that uses a For..Each loop but the second call does. The error occurs during the second call. See the "Steps to Reproduce Behavior" section below for more information. RESOLUTION ========== - Use a For..Next loop in place of the For..Each loop. -or- - Create your own collection class, an example of which can be found in the Visual Basic Books Online. Refer to the topic "Creating Your Own Collection Classes." STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available. MORE INFORMATION ================ Steps to Reproduce Behavior --------------------------- 1. Create a new ActiveX DLL project in Visual Basic. 2. Click Project1 Properties from the Project menu and set the Project Name to "AutoErr1." 3. Copy the following code to the Class1 module: Public Sub Method1() Dim col As New Collection Dim obj As Object For Each obj In col Next obj End Sub 4. Click "Make AutoErr1.dll" from the File menu to create the DLL file. 5. Quit Visual Basic. You may optionally save the project files. 6. Start Microsoft Excel 97 and create a new workbook. Press the ALT+F11 keys to start the Visual Basic editor. 7. Click Module from the Insert menu to add a standard module. Add the following code to the new module: Private Sub Test48() ' First call into dll Dim o as Object Set o = CreateObject("AutoErr1.Class1") Set o = Nothing ' Second call into dll Set o = CreateObject("AutoErr1.Class1") o.Method1 ' calls method that uses For..Each loop set o = Nothing End Sub 8. Press the F8 key to step through the Sub's code and note that the following error occurs when calling Method1: Run-time error '48': Error in loading DLL The following steps demonstrate how to reproduce the Automation error using Visual Basic as the client. Note that these steps may not reproduce the error on some machines. 1. Follow steps 1 through 5 above to create the AutoErr1.dll. 2. Start a new Standard EXE project in Visual Basic. Form1 is created by default. 3. Place 2 CommandButtons (Command1 and Command2) on Form1. 4. Copy the following code to the Form1 module: Private Sub Command1_Click() ' First call into dll Dim o As Object Set o = CreateObject("AutoErr1.Class1") Set o = Nothing End Sub Private Sub Command2_Click() ' Second call into dll Dim o As Object Set o = CreateObject("AutoErr1.Class1") o.Method1 Set o = Nothing End Sub 5. Press the CTRL+F5 keys to run the project. 6. Click Command1. 7. Click Command2. Note that the following error may occur when calling Method1: Runtime-error '-2147319765 (8002802b)': Automation Error Additional query words: element not found ====================================================================== Keywords : vb5all VBKBAX kberrmsg Version : WINDOWS:5.0,97sp2,97sp3 Platform : WINDOWS Issue type : kbbug Solution Type : kbpending ============================================================================= 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. Copyright Microsoft Corporation 1998.