How to set focus back on Access?

KitaYama

Well-known member
Local time
Tomorrow, 08:58
Joined
Jan 6, 2022
Messages
1,859
In after updated event of a text box, I use shell command to open a document (a pdf or drawing of the product) on a second monitor.
When the pdf is opened, Windows moves the focus to the recently opened document. So I have to click MS Access window again to set the focus on it and be able to continue inputting next text boxes.

Is there any way to bring back focus to Access window after opening a pdf file?

This is the code I'm using at present :
Rich (BB code):
Declare PtrSafe Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
   ByVal hwnd As Long, _
   ByVal lpOperation As String, _
   ByVal lpFile As String, _
   ByVal lpParameters As String, _
   ByVal lpDirectory As String, _
   ByVal nShowCmd As Long) _
   As Long

Public Sub PrintFile( _
                 ByVal FileName As String, _
                 Optional PrintShow As String = "Open")
    
    Call apiShellExecute(hWndAccessApp, PrintShow, FileName, vbNormalNoFocus, vbNullString, vbNormalNoFocus)

End Sub

Thanks for any kind of advice.
 
Just guessing but have you tried using SetFocus? For example:
Code:
Call apiShellExecute(...)
Me.SomeControlName.SetFocus
 
Just guessing but have you tried using SetFocus? For example:
Code:
Call apiShellExecute(...)
Me.SomeControlName.SetFocus
Yes. But nothing changes.
From Access point of view, the focus is on the specified control. I can see it because the pointer is blinking in the text box.
But from Windows point of view, the pdf file has the focus. So how many times I press Enter or other keys, the keys are sent to active window (pdf in this case) and not to Access.

I think I must find a way to change the focus between applications rather than focus between controls in Access.

Thanks
 
Yes. But nothing changes.
From Access point of view, the focus is on the specified control. I can see it because the pointer is blinking in the text box.
But from Windows point of view, the pdf file has the focus. So how many times I press Enter or other keys, the keys are sent to active window (pdf in this case) and not to Access.

I think I must find a way to change the focus between applications rather than focus between controls in Access.

Thanks
I see. I just gave it a try by opening the PDF in a minimized window, and the focus stayed on Access.
 
I need to see the pdf while inputting data in the form. If the pdf is minimized, there's no point in opening it.

Thanks anyway.
 
I need to see the pdf while inputting data in the form. If the pdf is minimized, there's no point in opening it.

Thanks anyway.
Okay, one last try... I don't usually use APIs. Can you try using FollowHyperlink to open the pdf?
 
Okay, one last try... I don't usually use APIs. Can you try using FollowHyperlink to open the pdf?
I'm not a fun of APIs too. I simply used it for the sake of vbNormalNofocus parameter to keep focus on Access. But it seems that's meaningless.

I used :
Code:
Application.FollowHyperlink FindDrawings(OrderProductFK.Column(1))

Still the same result. Key strokes are sent to Adobe and not to Access.
FindDrawings is a function to return the Drawing saved address based on its PK.
 
I'm not a fun of APIs too. I simply used it for the sake of vbNormalNofocus parameter to keep focus on Access. But it seems that's meaningless.

I used :
Code:
Application.FollowHyperlink FindDrawings(OrderProductFK.Column(1))

Still the same result. Key strokes are sent to Adobe and not to Access.
FindDrawings is a function to return the Drawing saved address based on its PK.
Sorry to hear it didn't work. When I tested it, the focus stayed with Access. However, I only tested it using the Immediate Window. Could you please test it again using the Immediate Window and pass a literal path to the pdf file, just to verify if the behavior is different when running code from a Form?
 
Sorry to hear it didn't work. When I tested it, the focus stayed with Access. However, I only tested it using the Immediate Window. Could you please test it again using the Immediate Window and pass a literal path to the pdf file, just to verify if the behavior is different when running code from a Form?
I'm sorry for this stupid question. But how can I check it.
I typed Application.FollowHyperlink "Z:\Drawings\N\J\1\NJ11146BJ004.pdf" in immediate window and pressed the Enter key.
The drawing is opened. How can I test where the focus is? If I press Enter key, the cursor moves to the next line in immediate window.

I don't think it shows that Access has the focus.
Or even if this proves that Access keeps the focus after FollowHyperlink, how we can implement it in a afterUpdate event?

thanks again.
 
I'm sorry for this stupid question. But how can I check it.
I typed Application.FollowHyperlink "Z:\Drawings\N\J\1\NJ11146BJ004.pdf" in immediate window and pressed the Enter key.
The drawing is opened. How can I test where the focus is? If I press Enter key, the cursor moves to the next line in immediate window.

I don't think it shows that Access has the focus.
Or even if this proves that Access keeps the focus after FollowHyperlink, how we can implement it in a afterUpdate event?

thanks again.
After the pdf opened, I pressed the Alt key to see which menu would activate. With the API, the Acrobat menu was highlighted. With the FollowHyperlink method, the Access VBA menu activated.
 
After the pdf opened, I pressed the Alt key to see which menu would activate. With the API, the Acrobat menu was highlighted. With the FollowHyperlink method, the Access VBA menu activated.
Well, mine is the same. ALT key highlights VBE's menu.
But if I move the exact command to after update of any textbox, pdf responds to key strokes. :cry:
 
How can I test where the focus is?

Consider before you start this app to change the Windows theme, perhaps to a custom scheme that emphasizes the active window border. Some of the more modern themes are a bit bland.
 
Well, mine is the same. ALT key highlights VBE's menu.
But if I move the exact command to after update of any textbox, pdf responds to key strokes. :cry:
Okay, thanks. That's all I was trying to confirm. So I guess Access behaves differently when running code from the VBA window.
 
By the way, I think there is an API for Bring Window to Front, but I have no experience with that. Good luck!
 
I just had one last thought before going to bed. If it's just for pdf files, perhaps another approach is to use a web browser control in a popup form to view the pdf file in Access.
 
I just had one last thought before going to bed. If it's just for pdf files, perhaps another approach is to use a web browser control in a popup form to view the pdf file in Access.
The point to get here is that when you execute the Shell command the focus is passed to that program, and Access has no control over the display run under Windows. My guess is that with the web browser control you could take the focus elsewhere but I have not tested this.

Jiri.
 
The point to get here is that when you execute the Shell command the focus is passed to that program
I had a feeling vbNormalNoFocus parameter is there to keep the focus on current application. Not the opened one.
But I'm afraid you are correct.
 
Searching for an answer, I ran across a reply that reminded me of a Windows Security issue. In general, if you do not possess elevated privilege, you might not be able to do this. In the middle of that post there is a cut/paste from MSDN documentation on the BringToFront method. Some of the methods discussed in the link require you to know the hWnd handle (which is a form property). However, it is possible that you would have to navigate a bit using .Parent properties to find the Access "parent window" to bring it to the front. Bringing a form to the front (by itself) would have the effect of bring the form to the front of the Access window but wouldn't move the Access window as a whole.


Here is another discussion from the Excel VBA side of things, but that should work about the same.


Finally, here is a different thread regarding the problem from C++.


In all cases, Windows has these pesky little internal security rules about who can do something to another process - which is what you are doing when you try to bring an app to the front. They seem to all point back to the Win32 API for that purpose, and from your discussion, your REAL problem is that the rule that put your shelled program in front isn't reversible. The shelled program could release the window or exit, but until one of those events occurs, it owns the front position.

EDIT: For clarity... using SHELL creates a new process, not a child process. That is a contributor to the difficulty. If it had been a child process, then Access DOES have sufficient control regarding BringToFront among its child windows (i.e. individual forms).
 
Million thanks Doc for your effort to help.
I'll go through your links this evening when I have a little bit free time and see if I can find a solution.
 

Users who are viewing this thread

Back
Top Bottom