Visual Basic Language Reference  

End Statement

Terminates execution immediately.

End

Remarks

The End statement can be placed anywhere in a procedure to end code execution, close files opened with an Open statement, and clear variables. The End statement calls the Exit method of the Environment class in the System namespace. System.Environment.Exit requires that you have SecurityPermissionFlag.UnmanagedCode permissions. If you do not, a SecurityException error occurs.

When executed, the End statement clears all variables at module and class level and all static local variables in all modules.

Note   The End statement stops code execution abruptly, without invoking the Finalize method or any other Visual Basic code. Object references held by other programs are invalidated.

The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created and no code executing.

With an additional keyword, End delineates the end of the definition of the appropriate procedure or block.

Example

This example uses the End Statement to end code execution if the user enters an invalid password.

Sub Form_Load()
   Dim Password, Pword As String
   PassWord = "Swordfish"
   Pword = InputBox("Type in your password")
   If Pword <> PassWord Then
      MsgBox ("Sorry, incorrect password")
      End
   End If
End Sub

See Also

End | Requesting Minimum Permissions | SecurityPermissionFlag Enumeration