Access application "switch to" event

  • Thread starter Thread starter JF
  • Start date Start date
J

JF

Is there a way to capture the event when an Access application is switched
to or refocused after using another application.

For example, say I have MS Word and my Access app open. I quickly go to
word, paste some text, then switch back to my Access app. Can I somehow
capture this event within Access VBA?

Thanks,

JF
 
| Is there a way to capture the event when an Access application is switched
| to or refocused after using another application.
|
| For example, say I have MS Word and my Access app open. I quickly go to
| word, paste some text, then switch back to my Access app. Can I somehow
| capture this event within Access VBA?
------------
Sorry but Access events are form-specific. There is the OnLostFocus and
OnGotFocus events for each and every form. But there is no such event for
the whole Access mdi application.

Hope this helps,
 
Thanks for the reply.

I've tried playing with the "Focus" events but they only seem to work when
focus changes *within* Access. Switching out of Access and back in does not
fire the GotFocus event for the form currently open.

Is this the expected behavior? If it did fire the form's GotFocus event,
that would resolve my issue.
 
| I've tried playing with the "Focus" events but they only seem to work when
| focus changes *within* Access. Switching out of Access and back in does
not
| fire the GotFocus event for the form currently open.
|
| Is this the expected behavior? If it did fire the form's GotFocus event,
| that would resolve my issue.
-------------
This behaviour is by design. If you switch from Access to another
application, the Lost focus event on the Access form does not fire because
from the context of Access the focus was not removed. In the same manner,
if you switch back to Access, from within Access the current form did not
really lose focus.

To capture Access loses focus and returns focus from another application,
you would need to write low-level Windows API level code. It can be done
but it is not a trivial task if you're not a Windows API programmer.

Hope this helps,
 
I've done some Win API programming before. After reading this, I've did a
little research on the topic. I'm guessing I would have to use the
SetWindowsHookEx function and try to capture the WM_ACTIVATEAPP message?

Could you possibly direct me to a site that has more information on how to
incorporate this into my app?

Thanks,

JF
 
| I've done some Win API programming before. After reading this, I've did a
| little research on the topic. I'm guessing I would have to use the
| SetWindowsHookEx function and try to capture the WM_ACTIVATEAPP message?
|
| Could you possibly direct me to a site that has more information on how to
| incorporate this into my app?
-----------
Checkout this:

HOWTO: Intercept Keyboard Input from Visual Basic
http://support.microsoft.com/?id=177992

Hope this helps,
 
Thanks, that helped a lot. But I have a couple issues. I've implemented
the code below (omitting the API declarations for brevity) and it doesn't
quite work as expected. I set the hook in my app's startup form "load"
event (which happens to be a logon form). After the user logs in, the main
screen is loaded. However, the main screen doesn't get painted until I
switch to another application and then back to my app. At this point, the
activate message gets sent to my hook as expected, however, it happens about
six times. Then, the main screen won't process any events - I can't click
buttons, or other controls. Additionally, I can't even minimize, or even
close the whole app. If I switch to another app, and back to my app, the
activate goes off again but I still can't do anything. After doing this
several times, my whole system slows down with 100% utilization - mostly
being used by Access. Any ideas what I'm doing wrong?

<BEGIN CODE>
Private Type CWPRETSTRUCT
lResult As Long
lParam As Long
wParam As Long
Message As Long
hWnd As Long
End Type

Public Const WH_CALLWNDPROCRET = 12
Public Const WM_ACTIVATEAPP = &H1C
Public Const HC_ACTION = 0
Global g_hHook As Long

Public Function HookWindowProcRet(ByVal nCode As Long, ByVal wParam As Long,
_
ByVal lParam As Long) As Long
Dim oCWP As CWPRETSTRUCT

If nCode >= HC_ACTION Then
CopyMemory oCWP, ByVal lParam, Len(oCWP)
If oCWP.Message = WM_ACTIVATEAPP Then
If oCWP.wParam Then
MsgBox "App activated"
End If
HookWindowProcRet = 1
Exit Function
End If
End If
HookWindowProcRet = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function

'The following code is located in the start up form's load event:
Dim hThreadID As Long
hThreadID = GetCurrentThreadId()

g_hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, AddressOf
HookWindowProcRet, 0&, hThreadID)

<END CODE>
 
Back
Top