Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Location
    Netherlands
    Posts
    10

    Unanswered: capture errorlevel from a VBA shell

    In my Msaccess VBA module I want to open a shell which executes an external program.

    sSftp = "pscp.exe " & sDest & " name@192.168.5.1:" & "admin\backup\" & sDBNew
    Call Shell(sSftp, vbNormalFocus)

    This will send a copy of my database to a sftp server.

    Back in VBA, I want to check the errorlevel of this program. (0 = ok, 1 = error)

    Any ideas how to capture the errorlevel ?

    thanks
    Henny

  2. #2
    Join Date
    May 2009
    Posts
    258
    The following function from Chip Pearson might be just what you need:
    Shell And Wait

    Regards,

    Ax

  3. #3
    Join Date
    Jan 2003
    Location
    Netherlands
    Posts
    10
    Thanks for your reply Ax,
    That's a very interesting piece of code.
    There are some specific excel-objects in it, like XlEnableCancelKey, so I had to change it to comply to ms-access.

    I see the various results of the shell, but that's not what I need.
    I need the errorlevel of the program that ran in the shell.

    Keep searching...
    Thanks again.
    Henny

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You could use a batch file (.bat or .cmd) to launch the external program then assign the ERRORLEVEL returned by the .exe you would call in that batch to an environment variable or write it to a file you could open in your application, for instance:
    Code:
    @echo off
    <program.exe>
    If errorlevel==9 if not errorlevel==10 set EnvError=9
    If errorlevel==8 if not errorlevel==9 set EnvError =8
    If errorlevel==7 if not errorlevel==8 set EnvError =7
    If errorlevel==6 if not errorlevel==7 set EnvError =6
    If errorlevel==5 if not errorlevel==6 set EnvError =5
    If errorlevel==4 if not errorlevel==5 set EnvError =4
    If errorlevel==3 if not errorlevel==4 set EnvError =3
    If errorlevel==2 if not errorlevel==3 set EnvError =2
    If errorlevel==1 if not errorlevel==2 set EnvError =1
    If errorlevel==0 if not errorlevel==1 set EnvError =0
    See: Comment récupérer un errorlevel dans une variable?
    (Its in French, I could not find its English courterpart).
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •