Solved

Access form in foreground

Posted on 2011-02-11
7
901 Views
Last Modified: 2012-08-13
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 ?
0
Comment
Question by:luciepaul
  • 4
  • 2
7 Comments
 
LVL 21
Comment Utility
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?

0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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)
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
The key parameter is  HWND_TOPMOST  

mx
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:luciepaul
Comment Utility

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 ???
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
Comment Utility
Here is my AutoExec code that
1) Hides the Access App window
2) Forces a specific Form (frmLoader) on top of all other Windows.

Public Function APP_AutoExec() As Boolean

   'Hide the Application window
    Call ShowWindow(ByVal Application.hWndAccessApp, ByVal SW_HIDE)
   
   'The next two lines of code MUST appear in this order
   'Open Form on top of all other other windows.

    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)
     
End Function
This is the auto exec code for my database loader.  User must select 1 of N databases to load in 60 seconds.  Loader then copies db from server to local workstation, opens that app, then the Loader closes.

So, initially ... the Loader window is forced on top of all other windows, so it cannot get hidden behind other app windows that may be open.  In fact, I just added this functionality last week.

If you download Peter's free app

http://www.peterssoftware.com/fsd.htm

you can see this code in action.  In fact, that is where I found the magic SetWindowPos API.

mx
0
 

Author Closing Comment

by:luciepaul
Comment Utility
Database Mx
You're right SetWindowPos & SetForegroundWindow are magic !!
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Yep ... cool stuff.

mx
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now