"Invalid Outside Sub," Copy/Paste to VB General Declarations

ID Number: Q79240

1.00

WINDOWS

Summary:

An "Invalid outside Sub or Function" error occurs in the VB.EXE

environment under the following conditions:

1. A Sub or Function is copied to the general Declarations section of

a form.

2. 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.

3. The program is run from the VB.EXE environment.

Microsoft has confirmed this to be a problem in Microsoft Visual Basic

programming system version 1.0 for Windows. We are researching this

problem and will post new information here as it becomes available.

More Information:

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 "Invalid outside

Sub or Function" 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

----------

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.

General 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).

Steps to Reproduce Problem

--------------------------

1. Run Visual Basic, or from the File menu, choose New Project (ALT, F,

N) if Visual Basic is already running.

2. Enter the following code in the general Declarations of Form1:

Sub Test1 ()

Print "Hello"

End Sub

3. Highlight the code for the Sub and press CTRL+INSERT to copy the

entire Test1 subprogram.

4. Switch to the general Declarations window.

5. Paste the code copied in step 3 above by pressing CTRL+INSERT.

6. Change the name of the Sub from Test1 to Test2.

7. From the Run menu, choose Start (ALT, R, S) to run the program.

The error occurs in the general Declarations section on the following

code fragment:

Print "Hello"

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 reference words: 1.00