How to set focus back on Access?

KitaYama

Well-known member
Local time
Today, 09:13
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.
 
If you are trying to get data from a pdf into Access, there are ways to do so...
I'm afraid it's not possible. I need to extract material and thickness from drawings. But when you output a file as pdf (Save as pdf) the pdf is like a scanned picture. There's no text, textbox or form to be accessed. If you still believe there may be a way, I'd be glad to hear how.
 
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.
I have checked an old application of mine with an API function which forces an Access popup form on top of all other windows. It does not conserve the focus but you may be able to use it for your setting. Simply click on the Access form control that you want to continue with.
Code:
'
  '  Make sure form stays on top and reverse that
  '
  'Private Sub Command1_Click()
  '       Dim lR As Long
  '       lR = SetTopMostWindow(Form1.hwnd, True)
  '    End Sub
  '----------------------------------------------
  'Private Sub Command2_Click()
  '       Dim lR As Long
  '       lR = SetTopMostWindow(Form1.hwnd, False)
  'End Sub
  '------------------------------------------------
  Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) _
         As Long

         If Topmost = True Then 'Make the window topmost
            SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, _
               0, FLAGS)
         Else
            SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, _
               0, 0, FLAGS)
            SetTopMostWindow = False
         End If
      End Function

Best,
Jiri
 
@arnelgp I couldn't resist and wait. I had to use TimeViewer and check it. Your solution worked.

At least my question proved several points.
1- I'm not the only one suffering from this problem.
2- Microsoft has put vbNormalNoFocus parameter there just as a joke.
3- What @Sun_Force said is true. You're a genius.
 

Users who are viewing this thread

Back
Top Bottom