Solved

Access form in foreground

Posted on 2011-02-11
7
916 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
ID: 34873516
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
ID: 34873566
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
ID: 34873569
The key parameter is  HWND_TOPMOST  

mx
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:luciepaul
ID: 34874217

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
ID: 34874400
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
ID: 34874890
Database Mx
You're right SetWindowPos & SetForegroundWindow are magic !!
0
 
LVL 75
ID: 34874904
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

777 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