ACC2: How to Detect When MS Access Is Activated or Deactivated
ID: Q124393
|
The information in this article applies to:
SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article describes how to detect and run code when Microsoft Access is
activated or deactivated. Switching to Microsoft Access from another
application activates Microsoft Access, and switching from Microsoft Access
to another application deactivates Microsoft Access.
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools provided
with Microsoft Access. For more information about Access Basic, please
refer to the "Building Applications" manual.
MORE INFORMATION
The following steps demonstrate how to create a form called
Access_Activation that will detect when Microsoft Access is activated or
deactivated. The form contains two empty procedures called Access_Activate
and Access_Deactivate into which you can insert the code you want to run
when Microsoft Access is activated or deactivated. When the
Access_Activation form is open in Form view, switching to Microsoft Access
from another application will cause the Access_Activate procedure to run.
Switching from Microsoft Access to another application will cause the
Access_Deactivate procedure to run.
The Access_Activation form must be open in order to detect when Microsoft
Access is activated and deactivated. To make sure the form is always open,
you could open the form in a hidden state in your database's Autoexec
macro. To open the form in a hidden state, set the Window Mode argument of
the OpenForm macro action to Hidden.
- Open any database and create a blank new form. Set the following
properties for the form:
OnTimer: [Event Procedure]
TimerInterval: 500
- From the View menu, choose Code. Type the following lines in the
form module's Declarations section.
NOTE: In the following sample code, an underscore (_) at the end of a
line is used as a line-continuation character. Remove the underscore
from the end of the line when re-creating this code in Access Basic.
Option Explicit
Declare Function GetActiveWindow Lib "User" () As Integer
Declare Function GetParent Lib "User" (ByVal hWnd As Integer) _
As Integer
Declare Function GetFocus Lib "User" () As Integer
- Enter the following code for the OnTimer property event procedure:
Sub Form_Timer ()
Dim RetVal As Integer
Dim CurrhWnd As Integer
Dim AccesshWnd As Integer
Static ActiveApphWnd As Integer
' Get the Microsoft Access window handle (hWnd).
AccesshWnd = GetAccesshWnd()
' Get the active application window handle (hWnd).
CurrhWnd = GetActiveApphWnd()
' The first time through, just record the current
' window handle.
If ActiveApphWnd = 0 Then
ActiveApphWnd = CurrhWnd
Exit Sub
End If
' Determine if the current window handle differs from the
' previous window handle (focus change).
If CurrhWnd <> ActiveApphWnd Then
' Record the current window handle.
ActiveApphWnd = CurrhWnd
' Determine if the current handle is the Microsoft Access
' handle (activate Microsoft Access?).
If ActiveApphWnd = AccesshWnd Then
Access_Activate
Else
Access_Deactivate
End If
End If
End Sub
- Enter the following procedures in the form module:
Sub Access_Activate ()
' Insert the code that you want to run when Microsoft Access
' is activated here.
End Sub
Sub Access_Deactivate ()
' Insert the code that you want to run when Microsoft Access
' is deactivated here.
End Sub
Function GetAccesshWnd ()
GetAccesshWnd = GetTopMosthWnd(Me.hWnd)
End Function
Function GetActiveApphWnd ()
GetActiveApphWnd = GetTopMosthWnd(GetActiveWindow())
End Function
Function GetTopMosthWnd (ByVal hWnd)
Dim hWndTopMost As Integer
hWndTopMost = hWnd
' Find the top window without a parent window.
While hWnd <> 0
hWndTopMost = hWnd
hWnd = GetParent(hWnd)
Wend
GetTopMosthWnd = hWndTopMost
End Function
- Close the module and save the form as Access_Activation.
How to Use the Access_Activation Form
The following steps demonstrate how to use the Access_Activation form:
- Open any database and create the Access_Activation form as described
above.
- Substitute the following Access_Activate procedure for the
Access_Activate procedure you created in step 4 above:
Sub Access_Activate ()
MsgBox "Microsoft Access was activated"
End Sub
- View the Access_Activation form in Form view.
- Press ALT+TAB to switch from Microsoft Access to another application.
- Press ALT+TAB to switch back to Microsoft Access. A message box with
the text "Microsoft Access was activated" is displayed.
REFERENCES
For more information about using the Declare Function, search on "Declare"
then "Declare statement", using the Microsoft Access Help menu.
Keywords : kbprg
Version : 2.0
Platform : WINDOWS
Issue type : kbhowto