Send an Exit Code from a VB Application - May 2001
and Executing from the IDE?

I recently needed to create an application to edit text files generated by various other systems. The application can run with a GUI and in batch mode. When run in batch mode the application needed to send an 'ErrorLevel' code to the command processor. The ErrorLevel code was then used to 'OK' the results of the batch execution.

I had never written an 'exit code' routine so I posed a question in one of the news groups. I got help from various people, including Bob O'Brien. To test the process I wrote a demo program. I cleaned-up the program and decided to make it the tip of the month. I also used a routine which I call 'IsExecutingFromIDE' in this application.

Demo Program

I wrote a VB 6 program to demonstrate the code. A screen shot of the demo:

The program can be run in GUI or batch mode. A screen shot of the program executing in batch:


In this example exit code 5 was returned.

The Batch File

@echo off
echo 2001/05/01 Demonstrate sending an exit Code from a VB application.
echo 2001/05/01 Copyright 2001, Larry Rebich, larry@buygold.net.
@echo on
ExitCode /e=5
@echo off
rem errorlevel codes must be tested in reverse order.
if errorlevel 9 goto Err9
if errorlevel 8 goto Err8
if errorlevel 7 goto Err7
if errorlevel 6 goto Err6
if errorlevel 5 goto Err5
if errorlevel 4 goto Err4
if errorlevel 3 goto Err3
if errorlevel 2 goto Err2
if errorlevel 1 goto Err1
echo ErrorLevel = 0 Returned, Do exit Code 0 Items.
goto exit
:Err1
echo ErrorLevel = 1 Returned, Do exit Code 1 Items.
goto exit
:Err2
echo ErrorLevel = 2 Returned, Do exit Code 2 Items.
goto exit
:Err3
echo ErrorLevel = 3 Returned, Do exit Code 3 Items.
goto exit
:Err4
echo ErrorLevel = 4 Returned, Do exit Code 4 Items.
goto exit
:Err5
echo ErrorLevel = 5 Returned, Do exit Code 5 Items.
goto exit
:Err6
echo ErrorLevel = 6 Returned, Do exit Code 6 Items.
goto exit
:Err7
echo ErrorLevel = 7 Returned, Do exit Code 7 Items.
goto exit
:Err8
echo ErrorLevel = 8 Returned, Do exit Code 8 Items.
goto exit
:Err9
echo ErrorLevel = 9 Returned, Do exit Code 9 Items.
:exit
echo off
echo End of batch file.
pause Press any key to continue.

Can't Run If UNC

I found that the batch file can't be run from a folder pointed to by a UNC. I got the following error messages:

Include in Your Application

Module Function Sample Call Optional  
modExitCode.bas ReturnExitCode ReturnExitCode 5  Required
modExecutingFromIDE.bas  IsExecutingFromIDE  IsExecutingFromIDE Optional

Warning

Calling ReturnExitCode causes an immediate end to your application. It is a destructive termination. You should close all files and do any other application 'clean-up' before calling ReturnExitCode. If you call ReturnExitCode while running from the VB IDE the system will 'crash'.

The Demo Program Uses modConnectToWebSite

The demo program uses modConnectToWebSite.bas to display this page. For details on the use of that module click here.

Download VB 6 Source Code

Click here to download the VB 6 source code [16k]

Previous Tip   Tip Index    Next Tip

Home

Updated 2001/05/17