How to fail or succeed a job step from a Visual Basic 6.0 program?
If you are executing Visual Basic programs via SQL Agent using the CmdExec job step type, the job step by default will always succeed. In order to programmatically make the job step succeed or fail, you have to do something extra. This article describes what you need to do to accomplish this.
Visual Basic programs are Windows programs (using WinMain as the entry point
for the application), however they always return a process exit code of 0 (zero)
which is interpreted as success by the CmdExec job step. No matter if the
application raises an error, they process exit code will be 0 (zero). This is
easy to verify using a very simple batch or command file, which prints the
process exit code.
The sample application is called EXIT.EXE, which is a Visual Basic application, if you are not doing anything special the batch file will always return 0 (zero) and you will have no means to distinguish success or failure of your program from the job step.
You can slightly change your Visual Basic program to set the process exit code, by using the Win32 ExitProcess function, which you need to import from KERNEL32.DLL. You call this function to exit / terminate your program and set the process exit code, so that the CmdExec job step can pick it up and you can use it to determine your job flow inside SQL Agent.
A sample Visual Basic program that just sets the process exit code, would like this:
Questions or problems regarding this web site
should be directed to