Microsoft KB Archive/79240

= BUG: Invalid outside Sub Error When Copy or Paste to General =

Article ID: 79240

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q79240



SYMPTOMS
An &quot;Invalid outside Sub or Function&quot; error occurs in the VB.EXE environment under the following conditions:


 * A Sub or Function is copied to the general Declarations section of a form.
 * The name of the Sub or Function copied to the general Declarations section is changed, or the original Sub or Function that was copied is deleted.
 * The program is run from the VB.EXE environment.



CAUSE
The problem occurs when you copy a subprogram to the general Declarations section with Sub subname and End Sub (or Function functionname ... End Function) included. If you change the name of the original or copied Sub (or Function), the error &quot;Invalid outside Sub or Function&quot; will occur at run time. After the error occurs, the Sub or Function header of the copied Sub will be missing.

This problem occurs because the Sub or Function that was changed is treated as the entry of a new procedure. The body of the Sub or Function and the End Sub (or End Function) statement are treated as an existing part of the general Declarations section and are left behind.

The behavior is identical when the Sub (or Function) that was copied is deleted. The Sub (or Function) heading of the copy, residing in the general Declarations section, is treated as a new Sub or Function entry.



WORKAROUND
Follow these steps to work around the problem:


 * 1) Select (highlight) the remaining code fragment in the general Declarations section.
 * 2) From the Edit menu, choose Cut (ALT, E, T).
 * 3) From the Procedure box, choose Test2.
 * 4) From the Edit menu, choose Paste to paste the code cut in step 2 above into the body of the Test2 subprogram.
 * 5) Delete the duplicate End Sub statement.

Use the following steps to copy a subprogram and avoid the problem:


 * 1) Create a new subprogram (such as Sub Test1).
 * 2) Create a second subprogram with a different name (such as Sub Test2).
 * 3) Copy just the body of the code from the first subprogram (Test1) into the second subprogram (Test2).



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.



Steps to Reproduce Problem
 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.  Enter the following code in the general Declarations of Form1: Sub Test1 Print &quot;Hello&quot; End Sub  Highlight the code for the Sub and press CTRL+INSERT to copy the entire Test1 subprogram. Switch to the general Declarations window. Paste the code copied in step 3 above by pressing SHIFT+INSERT.</li> Change the name of the Sub from Test1 to Test2.</li> From the Run menu, choose Start (ALT, R, S) to run the program.</li></ol>

The error occurs in the general Declarations section on the following code fragment: Print &quot;Hello&quot; End Sub As illustrated above, the first line of the subprogram, Sub Test2, is missing. This is because Visual Basic treats the name change as a new Sub entry and established a new subprogram (Test2). The Procedure box will contain Test2 as a subprogram. Visual Basic considers the remaining part of Test2 (the code fragment above) to be an existing part of the general Declarations section.

Additional query words: buglist1.00 buglist2.00 buglist3.00 2.00 3.00

Keywords: kbbug KB79240

-

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

© Microsoft Corporation. All rights reserved.