
Automatically closing down Access using code after a Modal window has been closed
Even though all the code does is open the msgbox, once the message box is closed it still goes back
to the calling code and continues to run until it hits Exit Sub or End Sub.
I see where you are closing the dialog (msgbox) window. You then CloseAllForms if there was a pop-up
to close and quit Access. If you exit the sub instead of quitting can you then click the close
button (x) on the Access window to close Access successfully? If not, then quit probably isn't going
to work either. Have you verified that the pop-up is actually closing? Have you tried placing
DoEvents between CloseAllForms and Application.Quit to give everything a chance to complete before
the quit command hits? I would probably try one before CloseAllForms also.
In the CloseAllForms function, why a function instead of a sub since you aren't returning a value?
By chance could one of the forms you are trying to close be waiting for a response? There is no
acSaveNo in the DoCmd.Close statement.
I can't put my finger on anything definitive at this point, so hopefully some of this will help with
the troubleshooting.
--
Wayne Morgan
Microsoft Access MVP
Quote:
> Wayne
> Below is the code I used. The Modal window is a simple message box which is
> invoked on clicking a command button. All the code does is call MsgBox. I'm
> just using this as a test function to simulate a Modal window
> 'Check and Close any modal dialog boxes
> If CloseDialog = True Then
> 'Close down all form except this one
> CloseAllForms
> Application.Quit acQuitSaveAll
> End If
> *************************
> Function CloseAllForms()
> Dim nIndex As Integer
> Dim nCount As Integer
> nCount = Forms.Count - 1
> For nIndex = nCount To 0 Step -1
> If Forms(nIndex).Name <> "frmTimer" Then
> DoCmd.Close acForm, Forms(nIndex).Name
> End If
> Next
> End Function
> ****************************************************************************
> ***
> Private Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA"
> (ByVal hWnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long)
> As Long
> Private Declare Function apiGetLastActivePopup Lib "user32" Alias
> "GetLastActivePopup" (ByVal hWndOwner As Long) As Long
> Private Declare Function apiPostMessage Lib "user32" Alias "PostMessageA"
> (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
> As Long) As Long
> Private Const MAX_LEN = 255
> Private Const GW_HWNDNEXT = 2
> Private Const WM_CLOSE = &H10
> Function GetLastPopupWindowHandle() As Long
> GetLastPopupWindowHandle = apiGetLastActivePopup(hWndAccessApp)
> End Function
> Function CloseDialog() As Boolean
> Dim hWnd As Long
> On Error GoTo Err_handler
> Dim strClass As String
> Dim lngRet As Long
> hWnd = apiGetLastActivePopup(hWndAccessApp)
> strClass = GetClassName(hWnd)
> 'is this a modal window
> If strClass = "#32770" Then
> Call apiPostMessage(hWnd, WM_CLOSE, 0, 0)
> CloseDialog = True
> Else
> 'Not a modal window so return false
> CloseDialog = False
> End If
> Exit_Here:
> Exit Function
> Err_handler:
> CloseDialog = False
> Resume Exit_Here
> End Function