Solved Set Window Focus on current Database (1 Viewer)

Saphirah

Member
Local time
Today, 09:28
Joined
Apr 5, 2020
Messages
57
Hello everyone,

i am currently creating a vba solution for outlook to communicate with my access application.
I created a custom Button in the Outlook Header Bar, that calls access to open a form. So far so good.

Now i want vba to set the focus from outlook to access, so that i can immediately start writing, without having to click the access window first.

For this i used
Code:
AppActivate ProjectName
for ProjectName being the name that is displayed on top of the access application.
But the focus remains on outlook.
Maybe this happens because i am pressing the outlook button, so the code is running while the button is pressed. Maybe outlook takes back the focus once the code is finished and the button "released".


Is there maybe another way to do this? Am i doing something wrong?

Thank you very much for your help!
 

Isaac

Lifelong Learner
Local time
Today, 00:28
Joined
Mar 14, 2017
Messages
3,726
AppActivate is even less reliable and more finicky than SendKeys in my experience. Some apps tend to like being activated, like the infamous "Notepad.exe" used in all the examples out there. Some less so! IME when an app has been previously minimized entirely, it resists appactivate.

Also remember to play around with the title somewhat. For example, I just created a database and opened it. The only way I could get it to activate was to use:

AppActivate "Access"
 

Saphirah

Member
Local time
Today, 09:28
Joined
Apr 5, 2020
Messages
57
Hey, thank you very much for your help everyone. My problem is a bit more complicated though.

First, the following function is much more reliable, thank you @Gasman.
Code:
Private Declare PtrSafe Function FindWindow Lib "user32" _
                                    Alias "FindWindowA" _
                                    (ByVal lpClassName As String, _
                                     ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Sub ShowAccess()
   Dim hWnd As Long
   hWnd = FindWindow(vbNullString, "DatabaseProject")
   If hWnd > 0 Then SetForegroundWindow hWnd
End Sub

The function works on all other programs. Except on my access file. Let me elaborate further.

I created a button in the Outlook Header Bar (Sorry, don't know the name). When i click on it, i want to open a form in access. This works fine.
When i click the button access opens the form and the code for "activating the window" gets triggered. I can also see access getting the focus for a brief second.
1610975423456.png



But after the code execution of the button finished outlook takes back the focus from access. And due to all the functions of the form, my activation code etc getting called in the scope of this button press i don't know where to run my "switch window" code, so that it gets triggered after the button release.

I can not just use a "wait" function, because outlook will freeze until the wait function and the other code finished executing.

Any Ideas? Thank you very much for your help!
 

Saphirah

Member
Local time
Today, 09:28
Joined
Apr 5, 2020
Messages
57
AH yeah, when i test the function in access vba, without using the outlook button, my access gets the focus and i can write.
So the code is working, i got the caption right. Now it is just about when to execute the function so that outlook does not take the focus back...
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:28
Joined
Sep 21, 2011
Messages
7,717
I would start Googling for code that looks through all the active windows and if you find one with Access in it, use that.?
I have found that if you run it in debug mode then you do briefly go to Access and then back to the code.? :(

I have tested in in Outlook and Excel and the code does go to Access, just that I have no DB open.

Remember I said you have to be exact as to the caption of the window, hence looping through them all with a debug.print will get you the absolutely correct name.

Not something I have done I must admit, other that this situation here?
 

Saphirah

Member
Local time
Today, 09:28
Joined
Apr 5, 2020
Messages
57
I would start Googling for code that looks through all the active windows and if you find one with Access in it, use that.?
I have found that if you run it in debug mode then you do briefly go to Access and then back to the code.? :(

I have tested in in Outlook and Excel and the code does go to Access, just that I have no DB open.

Remember I said you have to be exact as to the caption of the window, hence looping through them all with a debug.print will get you the absolutely correct name.

Not something I have done I must admit, other that this situation here?
The problem is not the right name of the window. When i run the code it puts access in the foreground. So the code works, and the window name is correct.

The problem is the timing.

Code:
I press the button in outlook

    Access opens the Form

    Access runs the function -> gets the focus
    
The Button in Outlook gets released, Outlook receives the focus again.

So, how can i execute the "Set Access to focus" after the button in outlook is released?
I can not use the button event in any way, and i can not use the "Activate, Current" or other functions on startup.

Can i maybe use a timer function? Or is there a way to start a function "delayed" on a background thread?
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:28
Joined
Sep 21, 2011
Messages
7,717
No sure TBH.
I added a button to the Outlook menu bar to run that ShowAccess and I get that window on top and selected?
I would have thought that the window select should be the very last line of code in the sub?
 

Saphirah

Member
Local time
Today, 09:28
Joined
Apr 5, 2020
Messages
57
Okay. The problem was i added the "switch window" code in access, not in outlook. When you put it in outlook after your access function call the focus gets set correctly.
Thank you for your help Gasman.
 

Users who are viewing this thread

Top Bottom