Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access form in foreground

Posted on 2011-02-11
7
Medium Priority
?
944 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

885 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