Code Samples for Businesses, Schools & Developers

First Published 6 Nov 2025             Last Updated 7 Nov 2025


Whilst Task Manager allows you to determine which applications are running in the background, sometimes it can be useful to use VBA code to check whether another specified application is running. This can easily be done using Windows Machine Instrumentation (WMI)

CODE:


Public Function CheckProcess(ProcessName As String) As Integer

'Count number of instances of named process ProcessName
Dim strComputer As String
Dim colProcesses
Dim objWMIService

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

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

'Get all running processes with this ProcessName
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & ProcessName & "'")

CheckProcess = colProcesses.Count
' Debug.Print CheckProcess

End Function


If CheckProcess > 0 for the specified process, then that application is running.

For example, to check whether Excel is also running, use CheckProcess ("Excel.exe") and see if CheckProcess > 0.
To detect whether another instance of Access is running apart from the 'host' database, use CheckProcess ("MSAccess.exe") and check whether CheckProcess > 1.

This could be done when the application is first opened but doing that wouldn't detect when the specified app was opened subsequently.
As an alternative, use a Timer event on a hidden and always-open form to run a check at specified intervals e.g. every 5 seconds.

If a specified process is running, then you may wish to do one of the following:

a)   Show a warning message and ask whether the selected app should be closed

b)   Forcibly close the external app e.g. using taskkill, EndTask API, GetProcess API or a similar approach. See my article: Forcibly Shutdown Access
      The same methods can be used for any other process such as Word, Excel etc.
      For example using taskkill from a command prompt: cmd /c taskkill /F /IM "Excel.exe"

Excel terminated with TaskKill
      Similar code can be used within Access using the Shell command on e.g. a button click event procedure - in this case to 'kill' all instances of Notepad:


Private Sub cmdKillProcess_Click()
Shell "cmd /c taskkill /F /IM Notepad.exe"
End Sub


      NOTE:   Forcibly closing running apps can lead to loss of data and possible corruption. This approach should be treated as a last resort.

c)   An alternative approach combines both the above steps and was based on a suggestion by my Spanish colleague Xevi Batlle.
      The modified code both checks and kills all instances of a specified process (except for the current process)


Private Declare PtrSafe Function GetCurrentProcessId Lib "kernel32" () As Long

Public Function KillProcesses(ProcessName As String) As Boolean

'Kill all instances of named process ProcessName (except for the current process)

Dim iCount As Integer
Dim strComputer As String
Dim colProcesses
Dim objWMIService
Dim process

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

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

'Get all running processes with the specified ProcessName (except the current process if checking MsAccess.exe)
Set colProcesses = objWMIService.ExecQuery("SELECT *" & _
" FROM Win32_Process " & _
" WHERE Name = '" & ProcessName & "'" & _
" AND ProcessID <> " & GetCurrentProcessId & "")

'count instances of named process
iCount = colProcesses.Count

'Kill all instances
For Each process In colProcesses
KillProcesses = True
process.Terminate
Next

'OPTIONAL - show message
Select Case iCount

Case Is > 1
MsgBox iCount & " instances of " & ProcessName & " have been terminated", vbInformation, "KillProcesses"

Case 1
MsgBox iCount & " instance of " & ProcessName & " has been terminated", vbInformation, "KillProcesses"

Case 0
If ProcessName = "MsAccess.exe" Then
MsgBox "There were no other running instances of " & ProcessName, vbInformation, "KillProcesses"
Else
MsgBox "There were no running instances of " & ProcessName, vbInformation, "KillProcesses"
End If

End Select

End Function


d)   Save and quit the current Access application without losing any data.
      The following code checks for Excel, PowerShell and other Access instances. If any are found, the Access app is closed automatically.

      Example Code:


Private Sub Form_Timer()

On Error GoTo Err_Handler

'check for other instances of VBA / scripting enabled applications

'other Access apps
If CheckProcess("MSAccess.exe") > 1 Then GoTo PrepareShutdown

'check if Excel in use
If CheckProcess("Excel.exe") > 0 Then GoTo PrepareShutdown

'check if PowerShell ISE in use
If CheckProcess("powershell_ise.exe") > 0 Then GoTo PrepareShutdown

GoTo Exit_Handler

PrepareShutdown:
DoCmd.Close acForm, Me.Name, acSaveYes
Application.Quit

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & " in Form_Timer procedure : " & vbCrLf & Err.Description
Resume Exit_Handler

End Sub


      Code similar to that above is used in my Encrypted Split No Strings example database.
      However, in that example app, the code checks for a much wider range of running processes as one part of a number of security measures to prevent external automation being used.

App Running Check

Providing it is used with care, code such as CheckProcess (and similar variants) can be employed in a variety of situations to enhance security without risk of data loss.



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 7 Nov 2025



Return to Code Samples Page




Return to Top