PRB: Moving Off 'Sub' Doesn't Create 'End Sub'

Last reviewed: October 30, 1995
Article ID: Q129938
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SYMPTOMS

If you use the mouse or arrow keys to move off the statement:

   Sub X ()

the End Sub statement is not generated, and the Sub X() statement appears as part of the subroutine that follows it.

CAUSE

This is by design. The purpose of this behavior is to give you control over whether or not an End Sub is automatically generated. There are cases when it is desirable not to have an End Sub generated.

RESOLUTION

Leaving the Sub line by pressing the ENTER key automatically generates the appropriate End Sub. This also adds the End Sub where one did not exist if you previously left the line by using the mouse or arrow keys.

STATUS

This behavior is by design.

MORE INFORMATION

Visual Basic version 4.0 supports conditional compilation and you may want to take advantage of that to create more than one Sub statement for a single subroutine depending on various conditions.

   #if WIN32 then
   Sub MySubWin32version(x)
   #else
   Sub MySubWin16version(x,y)
   #endif

      ' ...
   End Sub

In this case, you would not want two End Sub statements generated for your two Sub statements. If you move off the first Sub statement with the ENTER key and the second Sub statement with the mouse or arrows, this procedure is generated correctly. As Visual Basic cannot anticipate which case will require End Sub and which will not, you are given explicit control.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Type the following line of code in the General Declarations section of Form1, but don't press the ENTER key:

       Sub Test()
    
    

  3. Use the mouse or arrow keys to move off the line. Sub Test does not get its own End Sub statement, but rather it is merged with the next Sub statement as in this example:

       Sub Test()
       Private Sub Form_Load()
    
       End Sub
    
    

  4. To add an End Sub, move to the end of the Sub Test() line, and press the ENTER key to separate the Sub Test() procedure from the Sub Form_Load() procedure:

       Sub Test()
    
       End Sub
    


Additional reference words: 4.00 vb4win vb4all
KBCategory: kbenv kbprg kbprb
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: October 30, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.