424,066 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,066 IT Pros & Developers. It's quick & easy.

How to bring a ms access form on top of all windows application

P: 66
My ms access file will capture the Idle time of the user. Now I would like to bring an animation on top of all application when the idle time is more than 5 seconds.

I have used the below code but it is not working for me.

Please advise if there are shell or API can be used to achieve this.


Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Compare Database
  3. Option Explicit
  4.  
  5.  
  6.       Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
  7.                                        ByVal hWndInsertAfter As Long, _
  8.                                        ByVal X As Long, _
  9.                                        ByVal Y As Long, _
  10.                                        ByVal cx As Long, _
  11.                                        ByVal cy As Long, _
  12.                                        ByVal wFlags As Long) As Long
  13.  
  14.  
  15.       Global Const HWND_TOPMOST = -1
  16.       Global Const SWP_NOSIZE = &H1
  17.       Global Const SWP_NOMOVE = &H2
  18.  
  19.       Private Declare Function BringWindowToTop Lib "user32" _
  20.  (ByVal lngHWnd As Long) As Long
  21.  Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  22.       Function TopMost(F As Form)
  23.          Call BringWindowToTop(F.hwnd)
  24.          SetForegroundWindow Application.hWndAccessApp
  25.       End Function
  26.  
  27. Function TopMost(F As Form)
  28.           Call SetWindowPos(f.hwnd, HWND_TOPMOST, 0, 0, 0, 0, _
  29.                            SWP_NOMOVE Or SWP_NOSIZE)
  30.          SetForegroundWindow Application.hWndAccessApp
  31.       End Function
  32.  
Dec 25 '14 #1

✓ answered by twinnyfo

I use this little function:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Declare Function SetForegroundWindow _
  5.     Lib "user32" _
  6.     (ByVal hwnd As Long) _
  7.     As Long
  8.  
  9. Public Function ActivateAccessApp() As Boolean
  10.     'Brings the DB to the front of all open windows
  11.     Dim appTarget As Access.Application
  12.  
  13.     Set appTarget = GetObject(CurrentDb.Name)
  14.     ActivateAccessApp = _
  15.         Not (SetForegroundWindow(appTarget.hWndAccessApp) = 0)
  16.     Set appTarget = Nothing
  17.  
  18. End Function
Create a new module and place teh above code in it. Just call the function from anywhere in your project like this:

Expand|Select|Wrap|Line Numbers
  1. Call ActivateAccessApp
Hope this hepps!

Share this Question
Share on Google+
10 Replies


twinnyfo
Expert Mod 2.5K+
P: 2,780
I use this little function:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Declare Function SetForegroundWindow _
  5.     Lib "user32" _
  6.     (ByVal hwnd As Long) _
  7.     As Long
  8.  
  9. Public Function ActivateAccessApp() As Boolean
  10.     'Brings the DB to the front of all open windows
  11.     Dim appTarget As Access.Application
  12.  
  13.     Set appTarget = GetObject(CurrentDb.Name)
  14.     ActivateAccessApp = _
  15.         Not (SetForegroundWindow(appTarget.hWndAccessApp) = 0)
  16.     Set appTarget = Nothing
  17.  
  18. End Function
Create a new module and place teh above code in it. Just call the function from anywhere in your project like this:

Expand|Select|Wrap|Line Numbers
  1. Call ActivateAccessApp
Hope this hepps!
Dec 29 '14 #2

P: 4
I am very dump with ACCESS right now. I created a module called module1 with the following codes.



Expand|Select|Wrap|Line Numbers
  1. Private Sub Command16_Click()
  2.         Call Module1
  3.  
  4.         MsgBox "Hello"
  5.  
  6. End Sub

How do I Call it from a form? When I created a button in a form, and simply type Call Module1. It says "Compiled Error.
Expected variable or procedure, not module."


@twinnyfo
3 Weeks Ago #3

twinnyfo
Expert Mod 2.5K+
P: 2,780
@pcheng

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command16_Click()
  2.  
  3.     Call ActivateAccessApp
  4.     MsgBox “Hello”
  5.  
  6. End Sub
Hope this hepps!
3 Weeks Ago #4

NeoPa
Expert Mod 15k+
P: 31,170
Where is SetForegroundWindow() defined/declared?
It wasn't available to me.
3 Weeks Ago #5

twinnyfo
Expert Mod 2.5K+
P: 2,780
This one is at work. I’ll have to post it on Wednesday. I must have forgotten that it refers to another function that needs to be declared.
3 Weeks Ago #6

NeoPa
Expert Mod 15k+
P: 31,170
: - )
3 Weeks Ago #7

twinnyfo
Expert Mod 2.5K+
P: 2,780
NeoPa,

I updated my response to include the properly declared funciton.
2 Weeks Ago #8

NeoPa
Expert Mod 15k+
P: 31,170
Good for you Twinny. Nice one :-)
2 Weeks Ago #9

P: 4
Thank you all for your help. This works great.
1 Week Ago #10

twinnyfo
Expert Mod 2.5K+
P: 2,780
Glad we could be of some hepp! Let us know if there are other challenges you face as you continue in your MS Access journey!
1 Week Ago #11

Post your reply

Sign in to post your reply or Sign up for a free account.