ACC2000: How to Make Microsoft Access Windows Stay on Top of Other Windows


Advanced: Requires expert coding, interoperability, and multiuser skills.


This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).


Summary


With the Microsoft Access Popup property, you can create forms that "float" (that is, they stay on top of other forms) in Microsoft Access. However, if you set focus to a program other than Microsoft Access, the Microsoft Access popup forms are overlapped. If you want your Microsoft Access popup forms to stay on top of windows of the other programs, you must call the Windows application programming interface (API) subroutine SetWindowPos().

More Information


Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Set a Form To Be Always On Top

To make your form stay on top of non-Microsoft Access forms and windows, call the following function from an event such as your form's On Open property.


NOTE: 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:
    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
  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. Type 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 new form or open an existing form and set the following properties:
    Popup: Yes
    OnTimer: =TopMost(Form)
    TimerInterval: 50
  5. Close the form and save any changes.

    Changing the Popup property in
    Design view has no effect unless you close and reopen your form.
  6. Open the form. Note that when you switch to any other program, this form stays on top of the other program.

Set Any Program Window To Be Always On Top

To float any program window on top of other program windows, you can use SetWindowsPos() together with the FindWindow() Windows API function as demonstrated below.

  1. Add the following API declarations to your Global Declarations section:
    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
  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. Type 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 Immediate Window, type the following command, and then press ENTER:


? Float_Calc()
NOTE: Calc.exe, the Calculator Accessory, is a program that comes with Windows and should be in your Windows directory.