Automatically closing down Access using code after a Modal window has been closed 
Author Message
 Automatically closing down Access using code after a Modal window has been closed

Dear All

I have an a front end Access 97 database on NT 4.0 SP6a which I want to
close down automatically so that I can perform maintenance on the back-end
database.  I have got some code that does this using the Application.Quit
which works perfectly as long as there is no Modal window open in the
database.  When there is a Modal window open I first use some code (called
"CloseDialog" which I got off a newsgroup) to close the Modal down.  This
works fine, however if I then try to close Access using Application.Quit it
does not work.  Instead Access is minimised on the task bar - somtimes I can
open it up other times I need to use End Task to terminate it.

Has anybody got any ideas as to why this is happening?

regards
Paul



Fri, 22 Oct 2004 18:51:59 GMT  
 Automatically closing down Access using code after a Modal window has been closed
That is probably not a problem with automatically closing Access, it
is probably a problem with closing Access in general.  Usually, this
results from failing to explicitly set references to database objects
= Nothing when done with them or trying to use a control reference as
a boolean expression in a VB conditional statement (e.g. If Me.cbYesNo
Then ...).

On Mon, 6 May 2002 11:51:59 +0100, "Paul Da Silva"

Quote:

>Dear All

>I have an a front end Access 97 database on NT 4.0 SP6a which I want to
>close down automatically so that I can perform maintenance on the back-end
>database.  I have got some code that does this using the Application.Quit
>which works perfectly as long as there is no Modal window open in the
>database.  When there is a Modal window open I first use some code (called
>"CloseDialog" which I got off a newsgroup) to close the Modal down.  This
>works fine, however if I then try to close Access using Application.Quit it
>does not work.  Instead Access is minimised on the task bar - somtimes I can
>open it up other times I need to use End Task to terminate it.

>Has anybody got any ideas as to why this is happening?

>regards
>Paul

--
Steve Jorgensen
Database application developer - available
http://www.coho.net/~jorgens


Fri, 22 Oct 2004 19:16:02 GMT  
 Automatically closing down Access using code after a Modal window has been closed

Quote:
> I have an a front end Access 97 database on NT 4.0 SP6a which I want to
> close down automatically so that I can perform maintenance on the back-end
> database.  I have got some code that does this using the Application.Quit
> which works perfectly as long as there is no Modal window open in the
> database.  When there is a Modal window open I first use some code (called
> "CloseDialog" which I got off a newsgroup) to close the Modal down.  This
> works fine, however if I then try to close Access using Application.Quit it
> does not work.  Instead Access is minimised on the task bar - somtimes I can
> open it up other times I need to use End Task to terminate it.

No clue offhand, could you post the CloseDialog code?
     -Deus


Fri, 22 Oct 2004 21:16:56 GMT  
 Automatically closing down Access using code after a Modal window has been closed
I wonder if you have a conflict trying to run code from 2 different procedures.

How was the modal window opened? If it was opened by code, the code that called it was stopped until
the window was closed. When you close the window the calling code would try to run again, the same
time as your shut down code is running.

--
Wayne Morgan
Microsoft Access MVP



Quote:
> Dear All

> I have an a front end Access 97 database on NT 4.0 SP6a which I want to
> close down automatically so that I can perform maintenance on the back-end
> database.  I have got some code that does this using the Application.Quit
> which works perfectly as long as there is no Modal window open in the
> database.  When there is a Modal window open I first use some code (called
> "CloseDialog" which I got off a newsgroup) to close the Modal down.  This
> works fine, however if I then try to close Access using Application.Quit it
> does not work.  Instead Access is minimised on the task bar - somtimes I can
> open it up other times I need to use End Task to terminate it.

> Has anybody got any ideas as to why this is happening?

> regards
> Paul



Fri, 22 Oct 2004 22:49:55 GMT  
 Automatically closing down Access using code after a Modal window has been closed
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



Fri, 22 Oct 2004 23:25:51 GMT  
 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





Sat, 23 Oct 2004 11:34:02 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Problems Closing a New Window using window.close()

2. closing access from code doesn't work, see further down

3. Access 97: Closing down because of code?

4. Determining if Exe is running, window has closed down, window has opened

5. closing the Yes/No window when closing a Shelled Window

6. Closing Parent Window when the child window closes

7. how to close Modal windows

8. Windows Forms Closes Automatically.

9. how to close another window automatically?

10. automatically close a pop up window

11. Restarting Windows 95 (And Closing all apps automatically)

12. closing chains of modal dialogs... - modal [1/1]

 

 
Powered by phpBB® Forum Software