4

I am using the Shell() function to execute an external application in MS Access. Shell() returns a task ID of the specific process that was started.

I need a way to check if that process is still running or if it has closed. I'm sure that I will need to do some sort of windows API call but I can't seem to find anything just now.

Icode4food
  • 8,504
  • 16
  • 61
  • 93

3 Answers3

3

This is an Access FAQ, and it was answered years and years ago on the FAQ site for comp.databases.ms-access, http://mvps.org/access/.

API: Shell and Wait

David-W-Fenton
  • 22,871
  • 4
  • 45
  • 58
2

Shell Tasklist (Description at http://technet.microsoft.com/en-us/library/bb491010.aspx) is a good way to do it manually. I'm not sure how you could interact with the list of PIDs by automation, though. That's a bit more difficult.

Tip: If you just want to kill a process, use Shell "TaskKill /F /IM ""excel.exe""" to kill all open instances of MS Excel. Or Shell "TaskKill /F /IM ""msaccess.exe""" to kill all open instances of MS Access (including the Access file that hosts your VBA script, unfortunately). This is how I end my standard issue Error Handler.

Edit

When you run it, you should something like: alt text

Community
  • 1
  • 1
PowerUser
  • 11,583
  • 20
  • 64
  • 98
  • When I run TaskList at the command prompt it isn't found. Is this an external application that I need to download? I can't seem to find any reference to this in the documentation. – Icode4food Oct 20 '10 at 20:30
  • Odd. Well, that link I provided is to the official documentation for WinXP. Are you one of those lucky few to work in a company that has an OS younger than 7 years old? – PowerUser Oct 20 '10 at 20:51
  • I am running XP Home. Maybe it is a Pro app? That doesn't make much sense to me but that is all I can think of! – Icode4food Oct 21 '10 at 11:43
0

I used the following as part of the WMI:

Public Function WaitForProcess(ProcessName As String, Optional ProcessID As Boolean = False)

    ' if you dont mind including the reference for Microsoft WMI Scripting V.1.2 Library, uncomment below
    ' Otherwise, it should work fine without the reference, just no intelasense

    UpdateStatus "Starting WaitForProcess function to wait for process: " & ProcessName

    Dim strComputer As String
    Dim colProcesses
    Dim objWMIService

    UpdateStatus "Use this computer to see processes on"
    strComputer = "."

    UpdateStatus "Impersonate this computer"
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    UpdateStatus "Get all running processes with a " & IIf(ProcessID = False, "Name", "ProcessID") & " of: " & ProcessName
    Set colProcesses = objWMIService.ExecQuery("SELECT * " & _
                                               "FROM Win32_Process  " & _
                                               "WHERE " & IIf(ProcessID = False, "Name", "ProcessID") & " = '" & ProcessName & "'")

    UpdateStatus "Waiting untill there are no more instances of this process shown in the process list"
    Do
        DoEvents
        ' Update the collection of processes returned

        Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE " & IIf(ProcessID = False, "Name", "ProcessID") & " = '" & ProcessName & "'")


    Loop Until colProcesses.Count = 0

    UpdateStatus "The " & IIf(ProcessID = False, "Process Name", "Process ID") & ": " & ProcessName & " concluded successfully. WaitForProcess Completed"

End Function

This works well for my project; however, it does require Windows Management Instrumentation.

Curtis Inderwiesche
  • 4,940
  • 19
  • 60
  • 82