メインのaccessに下記コードを入力してみたところ、特定のaccessを閉じることができました。これを応用して複数指定したら、複数のaccessを閉じるれかも…と思いましたが、その特定のaccessが開いていないときに一度開いて閉じるみたいな動きになるため、現在閉じようとしているaccesが開いているのか判定する必要があるのかもしれません。
private sub コマンド1_Click
Dim ac As Object
Set ac= GetObject("●●●.accdb")
ac.DoCmd.Quit
Set ac = Nothing
End Sub
1Option Compare Database
23Option Explicit
45Private Declare PtrSafe Function EnumWindows Lib "user32.dll" _
6 (ByVal lpEnumFunc As LongPtr, _
7 ByVal lParam As LongPtr) As LongPtr
8Private Declare PtrSafe Function GetClassName Lib "user32.dll" _
9 Alias "GetClassNameA" _
10 (ByVal hwnd As LongPtr, _
11 ByVal lpClassName As String, _
12 ByVal nMaxCount As LongPtr) As LongPtr
13Private Declare PtrSafe Function SendMessage Lib "user32" _
14 Alias "SendMessageA" _
15 (ByVal hwnd As LongPtr, ByVal Msg As LongPtr, _
16 ByVal wParam As LongPtr, lParam As Any) As LongPtr
1718Private Const WM_CLOSE = &H10
1920Private mMyhWnd As Long
2122' コールバック関数
23Public Function EnumWindowsProc(ByVal hwnd As Long, _
24 ByVal lParam As Long) As Long
2526 Dim strClassBuff As String * 128
27 Dim strClass As String
28 Dim lngRtnCode As LongPtr
2930 lngRtnCode = GetClassName(hwnd, strClassBuff, Len(strClassBuff))
31 strClass = Left(strClassBuff, InStr(strClassBuff, vbNullChar) - 1)
32 If strClass = "OMain" Then
33 If hwnd <> mMyhWnd Then
34 SendMessage hwnd, WM_CLOSE, 0, 0
35 End If
36 End If
37EnumPass:
38 EnumWindowsProc = True
39End Function
404142Sub CloseOtherAccess()
43 mMyhWnd = Application.hWndAccessApp
44 Dim lngRtnCode As LongPtr
45 lngRtnCode = EnumWindows(AddressOf EnumWindowsProc, ByVal 0&)
46End Sub
47
1Option Explicit
2'
3Public Declare Function OpenProcess Lib "KERNEL32" ( _
4 ByVal dwAccess As Long, _
5 ByVal fInherit As Integer, _
6 ByVal hObject As Long) As Long
78Public Declare Function TerminateProcess Lib "KERNEL32" ( _
9 ByVal hProcess As Long, _
10 ByVal uExitCode As Long) As Long
1112Public Declare Function CloseHandle Lib "KERNEL32" ( _
13 ByVal hObject As Long) As Long
1415Public Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _
16 ByVal hwnd As Long, lpdwProcessId As Long) As Long
1718Public Const SYNCHRONIZE = 1048576
19Public Const NORMAL_PRIORITY_CLASS = &H20&
20Public Const PROCESS_TERMINATE = &H1
2122Sub Test_Sample_Miniature()
2324 Dim objSet As Object
25 Dim obj As Object
26 Dim Locator As Object
27 Dim Server As Object
28 Dim lngProcessID As Long
29 Dim hProcess As Long
30 Dim MyAccessThreadID As Long
31 Dim MyAccessProcID As Long
32 Dim MyAccesshWndID As Long
3334 'Get MyAccessProcID
35 MyAccesshWndID = Application.hWndAccessApp
36 MyAccessThreadID = GetWindowThreadProcessId(MyAccesshWndID, MyAccessProcID)
37 If MyAccessThreadID = 0 Then
38 MsgBox "Error Get ProcessID hWnd=" & MyAccesshWndID
39 Exit Sub
40 End If
4142 'Get ALL Access ProccesID
43 Set Locator = CreateObject("WbemScripting.SWbemLocator")
44 Set Server = Locator.ConnectServer
45 Set objSet = Server.ExecQuery("Select * From Win32_Process")
4647 'Kill Other Access ProccesID
48 For Each obj In objSet
49 If InStr(obj.Name, "MSACCESS") > 0 Then
50 '
51 lngProcessID = obj.ProcessID
52 If MyAccessProcID <> lngProcessID Then
53 hProcess = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, True, lngProcessID)
54 Call TerminateProcess(hProcess, 0&)
55 Call CloseHandle(hProcess)
56 End If
57 '
58 End If
59 Next
6061 'Free Object
62 Set objSet = Nothing
63 Set obj = Nothing
64 Set Server = Nothing
65 Set Locator = Nothing
66End Sub
Dim strTitle
Dim tgFilename As String
'閉じるファイル名
tgFilename = "Database1"
' ウィンドウタイトルから対象ファイルを閉じる。
Call GetProcessNames(tgFilename)
Stop
end sub
'https://www.tetsuyanbo.net/tetsuyanblog/24245 より
Public Function GetProcessNames(tgFilename As String)
' Wordアプリケーションオブジェクトを生成する
Dim objWordProcess As Object
Set objWordProcess = CreateObject("Word.Application")
' タスクリストから表示中のものだけ取得する
Dim objTask As Object
For Each objTask In objWordProcess.Tasks
Do
' 非表示のプロセスはスキップする
If objTask.Visible = False Then
Exit Do
End If
' プロセス名
Debug.Print objTask.Name
If objTask.Name Like "" & tgFilename & "" Then
MsgBox "起動しています。"
'タスクを終了する。
objTask.Close
End If
Loop While False
Next
' オブジェクトを破棄する
Set objWordProcess = Nothing
End Function