Use VBA to kill a specific Access process (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 19:50
Joined
Aug 15, 2010
Messages
1,002
I have multiple Access processes running on the same pc, all using the Access 32 bit Runtime.

The process appears in Task Manager as "Microsoft Access (32 bit)".

I have located several code that kill ALL the processes such as the code at

MS Access - VBA - Kill a Process | DEVelopers HUT (devhut.net)

I have 2 questions:
1) The process appears to be named ".exe", so I would assume it searches for something like "Access.exe". In the case of the Runtime the process name is "Microsoft Access (32 bit)"

2) Can I identify the specific process that I want to kill (Whether by VBA or manually). For example, if I have 3 Access processes running and process #2 needs to be killed, can I identify the specific process?

Regards,
John
 
usually you can "see" the name of the db on the title bar.
you can use that title to kill the process.

Call killWindow("partialNameHere*")
Code:
Option Explicit

' Module Name: ModFindWindowLike
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 02/06/2005
'
' mODIFIED bY aRNELgP FOR x64 aCCESS
'
                  
#If VBA7 Then
    Private Declare PtrSafe Function EnumWindows Lib "user32" (ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Long
    Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
        
        'Could use global variables instead, but this is nicer.
        'Custom structure for passing in the parameters in/out of the hook enumeration function
        'Could use global variables instead, but this is nicer.
        Private Type FindWindowParameters
        
            strTitle As String  'INPUT
            Hwnd As LongPtr     'OUTPUT
        
        End Type
#Else
                    
    Private Declare Function EnumWindows Lib "user32" _
       (ByVal lpEnumFunc As Long, _
        ByVal lParam As Long) As Long
    
    Private Declare Function GetWindowText Lib "user32" _
        Alias "GetWindowTextA" _
       (ByVal hwnd As Long, _
        ByVal lpString As String, _
        ByVal cch As Long) As Long
        'Could use global variables instead, but this is nicer.
        'Custom structure for passing in the parameters in/out of the hook enumeration function
        'Could use global variables instead, but this is nicer.
        Private Type FindWindowParameters
        
            strTitle As String  'INPUT
            Hwnd As Long        'OUTPUT
        
        End Type
#End If

'''experimental''''''''''''''''
Private Const WM_CHAR = &H102
Private Const BM_CLICK As Long = &HF5&

''' close the window
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060

Private Const WM_CLOSE = &H10

#If VBA7 Then
Private Declare PtrSafe Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal Hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long

#Else
private Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If
'''''''''''''''''''''



#If VBA7 Then
Public Function FnFindWindowLike(strWindowTitle As String) As LongPtr

#Else
Public Function FnFindWindowLike(strWindowTitle As String) As Long
#End If
    
    'We'll pass a custom structure in as the parameter to store our result...
    Dim Parameters As FindWindowParameters
    Parameters.strTitle = UCase(strWindowTitle) ' Input parameter
    

#If VBA7 Then
    Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
#Else
    Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
#End If
    FnFindWindowLike = Parameters.Hwnd
    
End Function

#If VBA7 Then
Private Function EnumWindowProc(ByVal Hwnd As LongPtr, _
                               lParam As FindWindowParameters) As LongPtr
#Else
Private Function EnumWindowProc(ByVal Hwnd As Long, _
                               lParam As FindWindowParameters) As Long
#End If
   Dim strWindowTitle As String

   strWindowTitle = Space(260)
   Call GetWindowText(Hwnd, strWindowTitle, 260)
   strWindowTitle = UCase(TrimNull(strWindowTitle)) ' Remove extra null terminator
                                          
   If strWindowTitle Like lParam.strTitle Then
  
        lParam.Hwnd = Hwnd 'Store the result for later.
        EnumWindowProc = 0 'This will stop enumerating more windows
  
   Else

        EnumWindowProc = 1

   End If
                          
End Function

Private Function TrimNull(strNullTerminatedString As String)

    Dim lngPos As Long

    'Remove unnecessary null terminator
    
    lngPos = InStr(strNullTerminatedString, Chr$(0))
  
    If lngPos Then
        TrimNull = Left$(strNullTerminatedString, lngPos - 1)
    Else
        TrimNull = strNullTerminatedString
    End If
   'Debug.Print TrimNull
End Function

' arnelgp
Public Function killWindow(ByVal strWindowTitle As String)
#If VBA7 Then
    Dim Hwnd As LongPtr
#Else
    Dim Hwnd As Long
#End If
    Hwnd = FnFindWindowLike(strWindowTitle & "*")
    
    If Hwnd <> 0 Then
        Call SendMessageBynum(Hwnd, WM_CLOSE, 0, 0)
    End If
End Function
 
Only guessing here, but when you open each Access instance, doesnt TM suffix them with a number?
 
Only guessing here, but when you open each Access instance, doesnt TM suffix them with a number?
As indicated in the attachment, when you expand the Access process, the title of the Access db appears. In this case I opened Access db #24 (number is assigned by me) twice, so I guess it would be possible to kill all #24s
 

Attachments

  • TaskManager.jpg
    TaskManager.jpg
    34.8 KB · Views: 294
If you are able to ID the process then either Daniel's or Arnel's code should work. I have not tried either but I have full confidence in both.
 
Many thanks for all your help. If I can modify my original question to the following:
How can I programmatically kill a specific Access session from a group of Access sessions, running on a pc (not using VBA).

I will have several Access sessions running on a pc, each with a different FE and BE. I want to be able to kill a specific Access sessions, relating to a specific FE-BE combination. I do not mind killing all sessions which belong to the same FE-BE.

Using "taskkill /f /im msaccess.exe" will kill all Access sessions.

I can kill the process (specific Access session) using "taskkill /PID ProcessID /F".

Is there a way (not using VBA) to obtain the PID, assuming I know the FE-BE name and pathname?
 
Use the window title?
 
As I mentioned before, if say the Process ID is 12345, the following will kill the process

taskkill /PID 12345 /F

How can the process ID of a specific access session be obtained?
 
As I mentioned before, if say the Process ID is 12345, the following will kill the process

taskkill /PID 12345 /F

How can the process ID of a specific access session be obtained?
You need to determine which access window, you want to terminate, using that link?
Open a few databases and then look at each window title. They should all be different, unless you open the same DB twice?

Code:
MSACCESS.EXE                 11264 Console                    1     35,448 K Running         Acer7720\Paul                                           0:00:07 Microsoft Access - JAGTest : Database (Access 2007)            
MSACCESS.EXE                 11976 Console                    1     40,964 K Running         Acer7720\Paul                                           0:00:06 Commissions & Fees
 
Thanks Gasman.

Is the Windows title what appears in this attachment?

What command did you use to list the two Access sessions in your latest post?
 

Attachments

  • TaskManager.jpg
    TaskManager.jpg
    34.8 KB · Views: 299
As an exploratory thing, start the Windows Command Prompt and type TASKLIST /?

From there you should be able to see the many options to find information on running tasks, including the ability to filter using /FI option with a filter string.

Remember that if you want output, you can redirect it using the "> outputspec" option, as TASKLIST bunch-of-options >.\LISTFILE.TXT

Then you can use a SHELL sequence to create a file you can then read to determine your process ID
 
As an exploratory thing, start the Windows Command Prompt and type TASKLIST /?

From there you should be able to see the many options to find information on running tasks, including the ability to filter using /FI option with a filter string.

Remember that if you want output, you can redirect it using the "> outputspec" option, as TASKLIST bunch-of-options >.\LISTFILE.TXT

Then you can use a SHELL sequence to create a file you can then read to determine your process ID
@The_Doc_Man
I'd be just deleting the process using the window title, less hassle?

In fact before this thread, I di not have a clue as how to do it, now I do. :)

@arnelgp 's code does the same thing with VBA, which was what was originally and specifically asked for?
 
As an exploratory thing, start the Windows Command Prompt and type TASKLIST /?

From there you should be able to see the many options to find information on running tasks, including the ability to filter using /FI option with a filter string.

Remember that if you want output, you can redirect it using the "> outputspec" option, as TASKLIST bunch-of-options >.\LISTFILE.TXT

Then you can use a SHELL sequence to create a file you can then read to determine your process ID
Very useful. Many thanks.
 
@The_Doc_Man
I'd be just deleting the process using the window title, less hassle?

In fact before this thread, I di not have a clue as how to do it, now I do. :)

@arnelgp 's code does the same thing with VBA, which was what was originally and specifically asked for?
Hi Gasman, many thanks for your reply.

I have trouble identifying the windows title. I attached a file and asked whether that is what you mean by windows title.
 
Nothing attached.? :(

Use as Doc advises
Code:
tasklist /v > c:\temp\tasklist.txt ' assuming you have a c:\temp
to see what windows you have. Obviously have some access DBs open before you do that. Then you can see what the windows are called.?

Look at the options for either tasklist or taskkill with the /? parameter
I issue the commands from a command window?
 
To amplify this, I frequently used a shell command to find some information by dumping output to a file and then reading it, because such files are frequently fixed-format output (essentially, constant columns) so extracting data from them is just a MID$ function call. Thus, you just run the command, read the file one line at a time until you find what you want, then do your thing to the task in question, delete that output file, and you are done. Tedious, but easily managed with VBA if you figure out the right command for SHELL to operate.

Note the section in this reference that discusses asynchronous commands, because that will apply to my suggestion. There is a link in the paragraph on async operation that might help you figure out how to wait for the shell command to finish.

 
Nothing attached.? :(

Use as Doc advises
Code:
tasklist /v > c:\temp\tasklist.txt ' assuming you have a c:\temp
to see what windows you have. Obviously have some access DBs open before you do that. Then you can see what the windows are called.?

Look at the options for either tasklist or taskkill with the /? parameter
I issue the commands from a command window?
Gasman many thanks.

I ran on the same pc, 3 Access sessions (using the A13 runtime) and the result of the command you suggested is indicated in the attachment.

The first 2 instances of ACCESS.EXE belong to the same software and the third instance to a different software.

I cannot see any information that would identify either the first 2 sessions or the 3rd session. All 3 appear the same and I would like to be able to identify either the first 2 or the 3rd sessions and kill a problematic session.
 

Attachments

  • Tasklist.jpg
    Tasklist.jpg
    359.1 KB · Views: 318

Users who are viewing this thread

Back
Top Bottom