It is mdb file. Let me clarify further. I have two mdb files x.mdb and y.mdb. From x.mdb, I go to y.mdb. While I am in y.mdb, I am trying to kill x.mdb but x.mdb is open. Is there a way to close x.mdb from y.mdb through code? Thanks in advance for any help.
This should close all Open instances of Access other than the one it is being called from.
Paste this into a New Standard Module, save the Module as anything you like:
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetActiveWindow Lib "user32.dll" () As Long
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Public Const WM_CLOSE = &H10
Global ghWnd As Long
Global gCurrenthWnd As Long
Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
On Error Resume Next
Dim slength As Long, TitleBar As String
Dim retval As Long
Static winnum As Integer
winnum = winnum + 1
slength = GetWindowTextLength(hWnd) + 1
If slength > 1 Then
TitleBar = Space(slength)
retval = GetWindowText(hWnd, TitleBar, slength)
If InStr(TitleBar, "Microsoft Access" And gCurrenthWnd <> hWnd Then
If hWnd <> ghWnd Then
Call SendMessage(hWnd, WM_CLOSE, 0&, 0&)
End If
End If
End If
EnumWindowsProc = 1
End Function
To test/use it put this in the On Click event of a Button while you have other Access DBs open:
gCurrenthWnd = GetActiveWindow
Call EnumWindows(AddressOf EnumWindowsProc, 0)
Just put this together, can't guarantee it will work, but logically it should.
I tried Bill's code but it is not closing instance. I checked line by line. Everything seems to be working except Call SendMessage(hWnd, WM_CLOSE, 0&, 0&). This has no effect.
I tried Robert's DoCmd.Quit but it is closing everything including y.mdb.
AddressOf is COMPLETELY UNSUPPORTED by Microsoft in Office 97 environment. Use it at your own risk!!
Entering debug mode is not recommended as it is likely to cause problems (GPFs etc.).
Make sure you backup your work and save before running any such code. Using this technique adds another level of instability since there are so many different ways to set up things wrong. Once you get it to work properly, everything should be ok.
Make sure you enter a On Error Resume Next at the top of any callback function. This is done to ensure that any errors within a callback function are not propagated back to its caller.
Be careful of ByVal or ByRef when passing arguments to the function. If you don't get this right, nothing's going to work.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.