Microsoft KB Archive/194912

= FIX: Add Method of Forms Collection Fails in Executable =

Article ID: 194912

Article Last Modified on 3/7/2005

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q194912



SYMPTOMS
You are using the Add method of the Forms collection to load a form. The code works correctly in the Visual Basic development environment, but fails with the following error when the application is made into an executable:

Run-time error '-2147417848 (80010108)':

Automation error.



RESOLUTION
You can work around this problem by creating your own Forms collection. A reference to the form can be added to a collection, and keyed by the Form's name. This will let you load a form by specifying a text string and referencing the matching key field in the collection.

There are two options with this approach. You can pre-load the collection with a reference to all of the forms in the application at startup, which is the approach used in the following example. You may also load the forms into the collection as needed. There is slightly more programmatic overhead with the second approach, because you must check to see if the form is already in the collection. It is also important to note that adding the forms to the collection does not load the forms.

Steps to work around this problem are:  Start a new Standard EXE project in Visual Basic. Form1 is created by default. From the Project menu, add a second form to the project. From the Project menu, add a standard module to the project.  Add the following code to the code window of Form1: Private Sub Form_Click Dim strFormToLoad As String strFormToLoad = "Form2" loadForm (strFormToLoad) End Sub

Private Sub Form_Load colForms.Add Form2, "Form2" End Sub   Add the following code to Module1: Public colForms As New Collection

Public Sub loadForm(strFormName As String) Dim frm As Form Set frm = colForms(strFormName) frm.Show

End Sub  Run the project and click on Form1. Form2 should be loaded.</li> Stop the project.</li> Choose Make Project1.exe from the File menu.</li> Run Project1.exe, and click on Form1.</li> Form2 should load successfully.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This bug was corrected in Visual Studio 6.0 Service Pack 3. For more information about Visual Studio service packs, please see the following articles in the Microsoft Knowledge Base:

194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

194295 HOWTO: Tell That Visual Studio 6.0 Service Packs Are Installed

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Start a new Standard EXE project in Visual Basic. Form1 is created by default.</li> From the Project menu, add a second form to the project.</li> From the Project menu, add a standard module to the project.</li>  Add the following code to the code window of Form1: Private Sub Form_Click Dim strFormToLoad As String strFormToLoad = "Form2" loadForm (strFormToLoad) End Sub </li>  Add the following code to Module1: Public Sub loadForm(strFormName As String) Dim frm As Form Set frm = Forms.Add(strFormName) frm.Show

End Sub </li> Run the project, and click on Form1.</li> Form2 should be loaded.</li> Stop the project.</li> Choose Make Project1.exe from the File menu.</li> Run Project1.exe, and click on Form1.</li> You should receive the error message listed above.</li></ol>

Keywords: kbbug kbfix kbcode kbvs600sp3fix KB194912

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.