Microsoft KB Archive/184743

= PRB: Script Code Runs During AddCode Method =

Article ID: 184743

Article Last Modified on 7/16/2004

-

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 32-Bit Enterprise Edition
 * Microsoft Visual Basic for Applications 5.0

-



This article was previously published under Q184743



SYMPTOMS
Script code runs when you load code into the script control using the AddCode method.



CAUSE
The script contains statements outside of a procedure definition.



RESOLUTION
Remove any statements outside of procedure definitions.



STATUS
This behavior is by design.



MORE INFORMATION
With Active Scripting, unlike Visual Basic and most other languages, statements are allowed outside procedure definitions. This is to allow the script to automatically initialize itself at load time.

IMPORTANT: Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures.

Steps to Reproduce Behavior
 In Visual Basic, create a new project (Form1 is created by default). Click Components on the Project menu, and then select the "Microsoft Script Control 1.0" check box. Add the Script control (ScriptControl1), a text box (Text1), and a command button (Command1) to a form. Set the MultiLine property of the text box to TRUE. For ease of use, change the size of Text1 so that it accommodates 5 lines of 60 characters.  Add the following code to Form1: Private Sub Command1_Click On Error Resume Next With ScriptControl1 .Language = "VBScript" .AllowUI = True .AddCode Text1.Text End With End Sub  Run Form1 and open the Debug window.</li>  Type the following script in the text box: Sub Test MsgBox "Hello World" End Sub Test </li> Click the command button. The message box appears.</li> Remove the line "Test" from the script and run Form1 again. The Test code is not executed upon loading the code.</li></ol>

<div class="references_section">