bluenoser337
Programmer
I need the code for closing MSACCESS.EXE (Access 2000) from a button click, for example, within my VB application. Thanks!!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
'!!! PLACE THE FOLLOWING IN A FORM
Option Explicit
'
Private Sub cmdCloseMSAccessWindows_Click()
'-- Run a loop closing all "Microsoft Access" windows
Do Until CloseAccess = False
Loop
End Sub
'!!! PLACE THE FOLLOWING IN A MODULE
Option Explicit
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
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) 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
Private mlngHWNDAccess As Long
'
Public Function CloseAccess() As Boolean
Dim lngHWND As Long
'-- Get the HWND of a Microsoft Access window (if exists) and close it
lngHWND = GetAccessHWND
If lngHWND > 0 Then
Call SendMessage(lngHWND, WM_CLOSE, 0, 0)
'-- If window was close, return True so another close attempt will be made
CloseAccess = True
End If
End Function
Private Function GetAccessHWND() As Long
Dim lngSuccess As Long
'-- Enumerate though windows using a callback function
lngSuccess = EnumWindows(AddressOf FindAccessWindow, 58&)
GetAccessHWND = mlngHWNDAccess
'-- Reset modular variable
mlngHWNDAccess = 0
End Function
Public Function FindAccessWindow(ByVal lngHWND As Long, ByVal lParam As Long) As Long
Dim strReturn As String
Dim lngReturn As Long
If lParam = 58 Then '-- Enumerate windows
'-- Get this window's class name
strReturn = Space$(255)
lngReturn = GetClassName(lngHWND, strReturn, Len(strReturn))
strReturn = Left$(strReturn, lngReturn)
'-- Check if it is "OMain", the class name of Access's window
If StrComp(strReturn, "OMain") = 0 Then
'-- Get this window's text
strReturn = Space$(255)
lngReturn = GetWindowText(lngHWND, strReturn, Len(strReturn))
strReturn = Left$(strReturn, lngReturn)
'-- Check if it starts with "Microsoft Access"
If StrComp(Left$(strReturn, 16), "Microsoft Access") = 0 Then
'-- Set modular variable; exit function without returning "1" (this will end enumeration)
mlngHWNDAccess = lngHWND
GoTo Exit_Proc
End If
End If
FindAccessWindow = 1
End If
Exit_Proc:
Exit Function
End Function
Option Explicit
Private Sub cmdCloseMSAccessWindows_Click()
While CloseAccess
Wend
End Sub
Option Explicit
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
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) 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 mlngHWNDAccess As Long
Public Function CloseAccess() As Boolean
EnumWindows(AddressOf FindAccessWindow, 0)
If mlngHWNDAccess Then SendMessage mlngHWNDAccess, &H10, 0, 0: CloseAccess = True
End Function
Public Function FindAccessWindow(ByVal lngHWND As Long, ByVal lParam As Long) As Long
Dim strReturn As String, lngReturn As Long
strReturn = Space$(255)
If Left$(strReturn, GetClassName(lngHWND, strReturn, Len(strReturn))) = "OMain" Then
strReturn = Space$(255)
GetWindowText lngHWND, strReturn, Len(strReturn)
If Left$(strReturn, 16) = "Microsoft Access" Then mlngHWNDAccess = lngHWND Else FindAccessWindow = -1
End If
End Function
[blue]Option Explicit
Private Sub Command1_Click()
Dim myAccess As Object
Dim AllClosed As Boolean
' Closes all running instances of MS Access
Do Until AllClosed
On Error Resume Next
Set myAccess = GetObject(, "Access.Application")
If Err = 0 Then
myAccess.Quit
Else
AllClosed = True
End If
On Error GoTo 0
Loop
End Sub
[/blue]
Option Explicit
Private Sub Command1_Click()
Dim AllClosed As Boolean
On Error Resume Next
Do Until AllClosed
GetObject(, "Access.Application").Quit
If Err Then AllClosed = True
Loop
On Error GoTo 0
End Sub
Private Sub Command1_Click()
On Error GoTo 1
GetObject(, "Access.Application").Quit
Command1_Click
1 End Sub
Private Sub Command1_Click()
On Error GoTo 1
GetObject(, "Access.Application").Quit
1 End Sub
Private Sub Command1_Click()
On Error GoTo 1
GetObject(, "Access.Application").Quit
[b]Command1_Click[/b]
1 End Sub
Private Sub Command1_Click()
On Error GoTo 1
GetObject(, "Access.Application").Quit
Command1_Click
1 End Sub