Close another Access DB

Another thought.

I'm able to open a word application and then close it with GetObject(Word.appplication) and wordapp.quit. Is this something that we can use?

Don't know but you could try. As for the tool bar - no, not really it would still need the Window Handle.
 
I am able to get the window handle, hWnd, of AccDB2. I used the following code:

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
dim hWndAccDB2 as string
 
hWndAccDB2 = FindWindow(vbNullString, AccDB2-Caption)

Once AccDB2 is open, I can then obtain the hWnd for it. But I'm not sure how to then close AccDB2 using the hWnd. Any ideas?

Any help would be appreciated.
 
Okay I got it.

I open AccDB2 with ShellExecute(). In my case I control the caption of AccDB2 so its already known. I then find the Window Handle, hWnd, of AccDB2 thru the caption of AccDB2 using 'FindWindow'. Then using the hWnd, I close AccDB2 using 'SendMessage'. Please note you have to use the 'Private Const WM_CLOSE = &H10' to make this work.

To call it: Call SendMessage(hAccDB2, WM_CLOSE, 0, 0)

ModuleCode
Code:
Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
'------------------------Legend for above params.
'hWnd Don't Touch
'lpOperation - choice: edit, explore, find, open, print, Null
'lpFile - File Name
'lpParameters = generally don't touch
'lpDirectory = full Path to File
'lpShowCmd long interger = how it's shown, minimize, maximize, hide, etc.
'Example: ShellExecute(0, "open", FN, "", PTFile, 1)
 
Public Function Launch(FN As String, PTFile As String)
' FN = File Name
' PTFILE = Path to File
Launch = ShellExecute(0, "open", FN, "", PTFile, 1)
End Function


Form Code
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Dim hAccDB2 As String 'hWnd for AccDB2
 
Private Sub cmdOpenRx_Click()
    ' FN = File Name
    ' PTFILE = Path to File
    Launch FN, PTFILE
End Sub
 
Private Sub cmdCloseRx_Click()
        hAccDB2 = FindWindow(vbNullString, AccDB2-Caption)
        Call SendMessage(hAccDB2, WM_CLOSE, 0, 0)
End Sub
 

Users who are viewing this thread

Back
Top Bottom