ID: Q101325
The information in this article applies to:
Advanced: Requires expert coding, interoperability, and multiuser skills.
With the Microsoft Access Popup property, you can create forms that "float" (that is, they stay on top of other forms) within Microsoft Access. However, if you set focus to a non-Microsoft Access application, the Microsoft Access popup forms are overlapped. If you want your Microsoft Access popup forms to stay on top of other non-Microsoft Access windows, you must call the Windows application programming interface (API) subroutine SetWindowPos().
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.
NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.
To make your form stay on top of non-Microsoft Access forms, call the following function from an event such as your form's OnOpen property.
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.
You may have some Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive the duplicate procedure name error message, remove or comment out the declarations statement in your code.
1. Add the following API declarations to your global declarations section:
For Microsoft Access 7.0 and 97:
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
For Microsoft Access 1.x and 2.0:
Declare Sub SetWindowPos Lib "User" (ByVal hwnd%, _
ByVal hWndInsertAfter%, _
ByVal X%, ByVal Y%, ByVal cx%, _
ByVal cy%, ByVal wFlags%)
2. Add the following Constants to your global Declarations section:
Global Const HWND_TOPMOST = -1
Global Const SWP_NOSIZE = &H1
Global Const SWP_NOMOVE = &H2
3. Create the following Function:
Function TopMost (F As Form)
Call SetWindowPos(f.hwnd, HWND_TOPMOST, 0, 0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE)
End Function
4. Create a form and set the following properties.
In Microsoft Access versions 1.x, 2.0, and 7.0:
Popup: Yes
OnOpen: =TopMost(Form)
In Microsoft Access 97:
Popup: Yes
OnTimer: =TopMost(Form)
TimerInterval: 50
5. Close the form to save any changes; changing the Popup property in
Design view has no effect unless you reopen your form.
6. Open the form. When you switch to any other application, this form
should stay on top of the other application.
To float any other application on top of another application, such as floating the Windows Calculator, you can use SetWindowsPos() along with the FindWindow() Windows API function as demonstrated below.
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.
1. Add the following API declarations to your global declarations section:
For Microsoft Access 7.0 and 97:
Declare Function FindWindow% Lib "user32" Alias "FindWindowA" _
(ByVal lpclassname As Any, _
ByVal lpCaption As Any)
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
For Microsoft Access 1.x and 2.0:
Declare Function FindWindow% Lib "user" (ByVal lpclassname As Any, _
ByVal lpCaption As Any)
Declare Sub SetWindowPos Lib "User" (ByVal hwnd%, _
ByVal hWndInsertAfter%, _
ByVal X%, _
ByVal Y%, _
ByVal cx%, _
ByVal cy%, _
ByVal wFlags%)
2. Add the following Constants to your global Declarations section:
Global Const HWND_TOPMOST = -1
Global Const SWP_NOSIZE = &H1
Global Const SWP_NOMOVE = &H2
3. Create the following Function:
Function Float_Calc()
Dim X&, hwnd%
X = Shell("CALC.EXE", 1)
hwnd% = FindWindow%("SciCalc", 0&)
Call SetWindowPos(hwnd%, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or _
SWP_NOSIZE)
End Function
To demonstrate this, open the Debug Window (called Immediate Window in
Microsoft Access 1.x and 2.0) and enter the following command and then
press ENTER:
? Float_Calc()
NOTE: CALC.EXE is an application that comes with Windows and should be
in your Windows directory.
"Microsoft Windows Programmer's Reference, Volume 2: Functions," version 3.10
Additional query words: float
Keywords : kbprg FmsHowto
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto
Last Reviewed: November 21, 1998