Link to home
Start Free TrialLog in
Avatar of luciepaul
luciepaul

asked on

Access form in foreground

I have an Access application with the main Access Window hidden (users don't need it at all, all interactions thru forms)
All forms are in Modal = true an I display one form at a time.
Sometimes, when another non-Access window (ie Explorer) is open, when I close the active Access form and open a new one it stays on the background , besides IE ( for example)
What code lines do I have to add to open always the form on the foreground ?
Avatar of Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015
Flag of United States of America image

It is possible to show only one form at a time without using Modal = true

I rarely set Module to True unless it truly is necessary.

How are you opening your forms?

This is what I am using right now ...

The SetWindowPos  API call does what you  are wanting ...

   'Hide App window  ... makes form system modal
    Public Const SW_NORMAL = 1     ' Show Window
    Public Const SW_RESTORE = 9
    Public Const SWP_NOZORDER = &H4
    Public Const SWP_NOMOVE = &H2
    Public Const SWP_NOSIZE = &H1
    Public Const SWP_SHOWWINDOW = &H40
    Public Const HWND_TOP = 0
    Public Const HWND_TOPMOST = -1
    Public Const HWND_NOTOPMOST = -2
    Public Const SW_HIDE = 0       ' Hide window
    Public Declare Function SetForegroundWindow& Lib "user32" (ByVal hWnd As Long)
    Public Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
    Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Public 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


   'Hide the Application window
    Call ShowWindow(ByVal Application.hWndAccessApp, ByVal SW_HIDE)
   
   'The next two lines of code MUST appear in this order, such that the focus will go to lstDb on the Form.
   'Open Form on top of other modal windows.
    ''SetWindowPos Form_frmLoader.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
    SetWindowPos Form_frmLoader.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_SHOWWINDOW
   
   'Make Form the foreground window - open it in front of other application windows.
    SetForegroundWindow& (Form_frmLoader.hWnd)
The key parameter is  HWND_TOPMOST  

mx
Avatar of luciepaul
luciepaul

ASKER


To Database MX
Thank you , I try it.

I guess that in the line :
 ''SetWindowPos Form_frmLoader.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
there is no need for the " before SetWindowPos.  Am I right ???
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Database Mx
You're right SetWindowPos & SetForegroundWindow are magic !!