ACC2: How to Detect When MS Access Is Activated or DeactivatedID: Q124393
|
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.
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.
OnTimer: [Event Procedure]
TimerInterval: 500
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
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
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
Sub Access_Activate ()
MsgBox "Microsoft Access was activated"
End Sub
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
Last Reviewed: April 7, 1999