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

Last reviewed: June 21, 1995
Article ID: Q79240
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SYMPTOMS

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

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.

MORE INFORMATION

Steps to Reproduce Problem

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

  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 SHIFT+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: buglist1.00 buglist2.00 buglist3.00 1.00 2.00
3.00
KBCategory: kbenv kbbuglist
KBSubcategory: EnvtDes


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.