I thought I posted this before, but can’t find it anywhere. I’m taking a small break from my current projects to post some of my latest ‘tricks’. This is one that I am probably the most proud of. Ever wanted to put an Access form on top of all the other windows on your desktop? Well, Microsoft has a solution at : http://support.microsoft.com/default.aspx?…500&Product=acc%5B/url%5D. However, their solution uses a Timer event. The problem lies in the fact that Microsoft Access forms are very subclassed, so they don’t act like normal windows in many aspects. For example, if you made a form in VB, and wanted it to be ‘Always On Top’, you just use the SetWindowPos API, and set the form/window’s ZOrder to -1. Works like a charm. However, it doesn’t work like that in Access. Microsoft’s solution is to continuously call that API twice a second.
Yes, that works, but it’s a horrible way to do things.
Never fear, Drew has the solution for you. The secret is that even though Microsoft Access forms are very subclasses, the Microsoft Access window itself is not. It CAN be set to ‘Always On Top’. Unfortunately, who wants the entire Access window on top of everything else on their desktop. Not a problem, just set it’s ZOrder to -1, then HIDE IT! Yes, it works, and it keeps the ZOrder property while hidden. Better yet, all of the forms in that Access succession ‘inherit’ the Always On Top property (without a timer event firing). Now, in Access 97, all you have to do is set the form’s Popup proptery to true, and when you hide Access, the form sits on your desktop, with Access no where in site. In A2k and up, you used to have to set the Dialog Property to yes also. The down side to the Dialog property, is that is prevents the ZOrder code from working properly. Once again, I came up with a solution to that. The following code can be placed on any form (you’ll need a command button to fire it), in any version of Access (97+). Just set the Popup Property to Yes, and you’ll have a form that sits on top of everything else on your desktop:
Option Compare Database
Option Explicit
Private Declare Function SetWindowPos Lib “user32” (ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal _
cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function IsWindowVisible Lib “user32” (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib “user32” (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Sub cmdAlwaysOnTop_Click()
If IsWindowVisible(Application.hWndAccessApp) Then
ShowWindow Application.hWndAccessApp, SW_HIDE
SetWindowPos Application.hWndAccessApp, HWND_TOPMOST, 0, 0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE
ShowWindow Me.hwnd, SW_SHOW ‘Not needed in Access 97
Me.Repaint ‘Not needed in Access 97
Else
ShowWindow Application.hWndAccessApp, SW_SHOW
SetWindowPos Application.hWndAccessApp, HWND_NOTOPMOST, 0, 0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If IsWindowVisible(Application.hWndAccessApp) = False Then
ShowWindow Application.hWndAccessApp, SW_SHOW
SetWindowPos Application.hWndAccessApp, HWND_NOTOPMOST, 0, 0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE
End If
End Sub
I am attaching a screen shot of the effect. Notice that Outlook has the focus, however, there is an Access form (title frmRequests), which is just sitting on top of Outlook, happy as a pig in mud! .
Enjoy!