DOCUMENT:Q178357 22-DEC-1997 [vbwin] TITLE :HOWTO: Set an Error Level from a Visual Basic Application PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0 OPER/SYS:NT Win95 WINDOWS KEYWORDS:VB4WIN vb5all vb5howto ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 4.0, 5.0 on the following platforms: NT, Win95 --------------------------------------------------------------------- SUMMARY ======= This article contains a sample Visual Basic application that sets the DOS error level upon exiting and a DOS batch file for testing. MORE INFORMATION ================ 1. Start a new project and save as Project1. 2. In the Declaration Section of Form1, add the following declaration: Private Declare Sub ExitProcess Lib "kernel32" _ (ByVal uExitCode As Long) 3. Place three CommandButtons on the form (use default names). 4. Change the Caption property on the CommandButtons to 50, 100, and 150 respectively. 5. Place the following code behind the form: Private Sub Command1_Click() ExitProcess (50&) 'Button labeled "50" End Sub Private Sub Command2_Click() ExitProcess (100&) 'Button labeled "100" End Sub Private Sub Command3_Click() ExitProcess (150&) ''Button labeled "150" End Sub 6. Save the Project and Make the executable. 7. Create the following batch file named errlevel.bat in your project directory: echo off start /w project1 rem "start /w" if errorlevel 150 goto 150 if errorlevel 100 goto 100 if errorlevel 50 goto 50 echo Exit Code 0 goto Done :150 echo Exit Code 150 goto done :100 echo Exit Code 100 goto done :50 echo Exit Code 50 :done echo done 8. Run the batch file. When the user clicks on a CommandButton, the application will terminate with the error level on the button. The batch file will then test the error level and echo the result to the screen. Additional notes: 1. Start /w is necessary for this to work correctly in Windows 95. 2. "if errorlevel" must be in the order presented. "If the EXIT status is less than the specified value, the specified DOS command is executed; otherwise, processing continues with the next batch file command." REFERENCES ========== MS-DOS Batch Files, Second edition, MS Press ====================================================================== Keywords : VB4WIN vb5all vb5howto Version : WINDOWS:4.0,5.0 Platform : NT Win95 WINDOWS Issue type : kbhowto ============================================================================= 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. Copyright Microsoft Corporation 1997.