Microsoft KB Archive/143311

= INFO: Form Events and Sub Procedures Can Be Exposed =

Article ID: 143311

Article Last Modified on 5/7/2003

-

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
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q143311



SUMMARY
Starting with Visual Basic 4.0, forms are public. Therefore, the form events and sub procedures are prefaced by the private keyword so they are not publicly visible members of the form.



MORE INFORMATION
In Visual Basic 3.0, a form's events and sub procedures were private to the form, and could not be accessed outside of the form's scope. Because a form is public in Visual Basic 4.0 and higher, its events and sub procedures can be exposed by either prefacing it with Public or by removing Private from the event.

Step-by-Step Example
The following example works in Visual Basic 4.0 and higher but not in Visual Basic 3.0. To create the program, do the following:

 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.  In Form1 enter the following code: Sub DispMsg MsgBox "Inside Sub" End Sub   Add a module to the project and in Module1 enter the following code: Sub main 'some other code stuff could go here Call Form1.DispMsg End Sub  For VB4, select the Tools, Options menu item and the Project tab. Set Startup Form to Sub Main. For VB5/6: Select the Project, Properties menu item and the General tab. Set the Startup Object to Sub Main. Run this program. The Module1 Main sub procedure calls the DispMsg sub procedure in the form.

NOTE: Invoking an event through code is not good programming practice. However, you can get at routines and form events if needed. Just ensure that they are visible members of the class (the form in this case).</ol>

Additional query words: kbVBp400 kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport KbVBA kbControl

Keywords: kbinfo KB143311

-

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

© Microsoft Corporation. All rights reserved.