Public ENDSESSION As Boolean Private Declare Function CallWindowProc Lib "user32" _ Alias "CallWindowProcA" _ (ByVal lpPrevWndFunc As Long, _ ByVal hWnd As Long, _ ByVal msg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) _ As Long Private Declare Function SetWindowLong Lib "user32" _ Alias "SetWindowLongA" _ (ByVal hWnd As Long, _ ByVal NIndex As Long, _ ByVal wNewWord As Long) _ As Long Private Declare Function GetWindowLong Lib "user32" _ Alias "GetWindowLongA" _ (ByVal hWnd As Long, _ ByVal NIndex As Long) _ As Long Private Const GWL_WNDPROC As Long = (-4) Private Const WM_QUERYENDSESSION = &H11 Private PrevWindowProc As Long Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Select Case uMsg Case WM_QUERYENDSESSION If wParam <> 0 Then Else 'Msgbox "WM_QUERYENDSESSION message received" ENDSESSION = True End If End Select WindowProc = CallWindowProc(PrevWindowProc, hWnd, uMsg, wParam, lParam) End Function Public Sub UnsubClass(hWnd As Long) If PrevWindowProc <> 0 Then Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWindowProc) PrevWindowProc = 0 End If End Sub Public Sub SubClass(hWnd As Long) ENDSESSION = False PrevWindowProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc) End Sub Private Sub Form_Close() UnsubClass Application.hWndAccessApp End Sub Private Sub Form_Open(Cancel As Integer) SubClass Application.hWndAccessApp End Sub Private Sub Form_Unload(Cancel As Integer) If Not ENDSESSION Then If MsgBox("Close Application?", vbYesNo) = vbNo Then Cancel = True End If End If End Sub