HOWTO: Set an Error Level from a Visual Basic Application

ID: Q178357


The information in this article applies to:


SUMMARY

This article contains a sample Visual Basic application that sets the error level upon exiting and a DOS batch file to test the result. Visual Basic applications can also retrieve the error level returned by another Windows application using the technique shown in Q129796 listed below.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new Standard EXE project. Form1 is created by default.


  2. Place three CommandButtons on the form (use the default names).


  3. Add the following code to the General Declarations section of Form1:
    
          Private Declare Sub ExitProcess Lib "kernel32" _
          (ByVal uExitCode As Long)
    
          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
    
          Private Sub Form_Load()
             Command1.Caption = "50"
             Command2.Caption = "100"
             Command3.Caption = "150"
          End Sub
     


  4. Save your Project and Make an executable named Project1.EXE.


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


  6. Run the batch file. When the user clicks on a CommandButton, the application will terminate with the error level shown on the button. The batch file will then test the error level and echo the result to the screen.


Additional notes:


REFERENCES

Q129796 : HOWTO: 32-Bit App Can Determine When a Shelled Process Ends

MS-DOS Batch Files, Second edition, MS Press

Additional query words: kbVBp400 kbVBp500 kbVBp kbDSupport kbdse kbVBp600 kbNoKeyWord


Keywords          : 
Version           : 
Platform          : 
Issue type        : kbhowto 

Last Reviewed: May 14, 1999