Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to bring a window to the foreground 2

Status
Not open for further replies.

Eutychus

Programmer
Nov 14, 2007
61
US
I have an Access 2016 application that opens Notepad++ with the Shell function when the user clicks on a text box with a path in it. The Shell function then opens Notepadd++ and opens the document found in the path. That works fine. However, Notepad++ with the opened document is in a window behind the Access application window. I have not been able to find a way to bring it in front of the Access app window. I've searched many times and the answer I've come up with is what I'm using. It activates the window it seems but does not bring it to the foreground. Here is the code that is in the On Click event of the text box that holds the file path:
Private Sub FindPath_Click()
Dim dbPath As String
dbPath = Me.TextBoxPath ' TextBoxPath is the name of the text box with the path in it
Notepad = Shell("C:\Program Files\NotepadPlus\Notepad++.exe " & Chr(34) & dbPath & Chr(34), vbMaximizedFocus)
AppActivate Notepad
End Sub
Note: the Chr(34) is to put quotes around the path to handle spaces in the file name.
The value in the textbox would be something like: D:\FolderA\FolderB\This is my document.txt

Does anyone know how to bring the Window with Notepad++ opened in it to the foreground in front of the Access app?
Thank you much!


 
In the example code the Private Sub should have been named TextBoxPath_Click(). My apologies.
 
This piece of code shows me a small txt file in Notepad++ on top of other applications:

Code:
Dim lngMaVal As Long
Dim dbPath  As String

dbPath = "C:\TEMP\test.txt"

lngMaVal = Shell("C:\Program Files (x86)\Notepad++\notepad++.exe " & dbPath, 1)


---- Andy

There is a great need for a sarcasm font.
 
Thanks Andrzejek and strongm, but it does not work for me. I see you dimmed lngMaVal as long and you changed the Shell windowstyle argument from maximized to normal. I tried that and it still opens the Notepad++ application window behind my Access application window. I wonder if it has to do with where the code is located. My code is in the event procedure in the on_Click event of the text box control on a form. Is that where you placed yours? I also tried creating a separate module with a Function called OpenTextFile() and put the same code in there. I called it from the On_click event of the text box on the form and it still opens the window behind the Access app. I noticed that initially, the Notepad++ window is on top, but that lasts for a split second and then the Access app is pulled on top. Sometimes it is too quick to even see. This has me baffled. There is something keeping the Notepad++ window from being on top. Any further ideas?
 
I had my code in command button's Click event. Try that.

Maybe TextBox's Click event needs to stay in focus because Access assumes you will type something in it... [ponder]


---- Andy

There is a great need for a sarcasm font.
 
Andy, that worked! I put the code in a command button's click event and Notepad++ stayed on top. I'll have to modify my design a bit, but I can make that work. Thank you much!!!
 
>My code is in the event procedure in the on_Click event of the text box control on a form

I tested both your code and Andy's code under exactly that scenario. Both worked fine.

Are you sure you don't have any other events firing that are causing focus to switch back to the Access form?
 
strongm, Thank you for your testing and the "Are you sure..." question. I looked further and found that the text box "Is Hyperlink" property had been set to Yes. I changed it to No and--what do you know!--the Notepad++ app window stayed on top. I think that was the culprit all along. Thank you for your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top