I need to use this code to close my db if a form is still open, where does it go

  • Thread starter trevorC via AccessMonster.com
  • Start date
T

trevorC via AccessMonster.com

For anyone interested I found the answer I was looking for

When I start my program I "Hook" the Access Application Handle

'Hook the Access application so we can determine if it is shutting down
Hook Application.hWndAccessApp

Then When someone clicks the X or does ALT-F4 the function below WindowsProc
intercepts the WM_CLOSE message sent to the application window and blah blah
blah I get the desired result.

Option Explicit
'************************************************************
'API
'************************************************************

Private Declare Function CallWindowProc Lib "user32.dll" 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.dll" Alias
"SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

'************************************************************
'Constants
'************************************************************

Private Const GWL_WNDPROC = -4
Private Const WM_DESTROY = &H2
Private Const WM_CLOSE = &H10

'************************************************************
'Variables
'************************************************************

Private hControl As Long
Private lPrevWndProc As Long

'*************************************************************
'WindowProc
'*************************************************************
Private Function WindowProc(ByVal lWnd As Long, ByVal lMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'Selects which messages you want to detect
Select Case lMsg
Case WM_CLOSE
'Send the message we found
WindowProc = CallWindowProc(lPrevWndProc, lWnd, lMsg, wParam,
lParam)

'Unhook we don't want any more messages about this application.
Got the one we were
'looking for
Unhook

'Set the exiting application flag which allows frmLookup to
close
bExitingApp = True

'Close frmLookup if it is loaded
If IsLoaded("frmLookup") Then
DoCmd.Close acForm, "frmLookup"
End If

'Quit Access
Application.Quit
Exit Function
End Select
'Sends message to previous procedure
'This is VERY IMPORTANT!!!
WindowProc = CallWindowProc(lPrevWndProc, lWnd, lMsg, wParam, lParam)

End Function

I found this code here, and it looks like it will do what I need it to do -
Close DB and if a form is open close it first.
But I can't work out were to put it or how to activate it.
any help will appriciated.
TrevorC.

'*************************************************************
'Hook
'*************************************************************
Public Sub Hook(ByVal hControl_ As Long)
hControl = hControl_
lPrevWndProc = SetWindowLong(hControl, GWL_WNDPROC, AddressOf
WindowProc)
End Sub

'*************************************************************
'Unhook
'*************************************************************
Public Sub Unhook()
Call SetWindowLong(hControl, GWL_WNDPROC, lPrevWndProc)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top