Solved

Access form in foreground

Posted on 2011-02-11
7
927 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 - Microsoft MVP, Access and Data Platform) 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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