Opening Subfolder in Windows Explorer from access form (1 Viewer)

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Please let me know how to open subfolder from access form. I am able to open MyDocument but not able to open MyDocument /Cases/2020

Thanks for your help
 

Ranman256

Well-known member
Local time
Today, 12:47
Joined
Apr 9, 2015
Messages
4,337
Paste this code into a module, and it will open ANY file in its native application. File folders too.
In a form put the field and a button to open it.

if the file is myFile.pdf, will open it in acrobat
if the file is myFile.doc, it will open the doc in Word
if its just a file path, it will open in file explorer.
etc..
usage:
OpenNativeApp txtBox


paste this code into a module
Code:
'Attribute VB_Name = "modNativeApp"
'Option Compare Database
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Public Sub OpenNativeApp(ByVal psDocName As String)
Dim r As Long, msg As String
r = StartDoc(psDocName)
If r <= 32 Then
'There was an error
Select Case r
Case SE_ERR_FNF
msg = "File not found"
Case SE_ERR_PNF
msg = "Path not found"
Case SE_ERR_ACCESSDENIED
msg = "Access denied"
Case SE_ERR_OOM
msg = "Out of memory"
Case SE_ERR_DLLNOTFOUND
msg = "DLL not found"
Case SE_ERR_SHARE
msg = "A sharing violation occurred"
Case SE_ERR_ASSOCINCOMPLETE
msg = "Incomplete or invalid file association"
Case SE_ERR_DDETIMEOUT
msg = "DDE Time out"
Case SE_ERR_DDEFAIL
msg = "DDE transaction failed"
Case SE_ERR_DDEBUSY
msg = "DDE busy"
Case SE_ERR_NOASSOC
msg = "No association for file extension"
Case ERROR_BAD_FORMAT
msg = "Invalid EXE file or error in EXE image"
Case Else
msg = "Unknown error"
End Select
' MsgBox msg
End If
End Sub

Private Function StartDoc(psDocName As String) As Long
Dim Scr_hDC As Long
Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function

usage:
Code:
sub btnOpenFile_click()
OpenNativeApp ME.txtBox
end sub
 

vba_php

Forum Troll
Local time
Today, 11:47
Joined
Oct 6, 2019
Messages
2,880
you need to use the file dialog object in VBA. see here: https://docs.microsoft.com/en-us/office/vba/api/excel.application.filedialog

also called MSO FOLDER PICKER. it sounds like you're already using that. if you want to CHOOSE a location for it to open, throw the value of a textbox to your VBA code. you should also note that this object in VBA is completely unreliable, because it has compatibility issues with windows versions, as I have noticed by testing on multiple versions of the windows operating system.
 

vba_php

Forum Troll
Local time
Today, 11:47
Joined
Oct 6, 2019
Messages
2,880
Paste this code into a module, and it will open ANY file in its native application. File folders too.
In a form put the field and a button to open it.
that code is 1,000 lines longer than what is necessary
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:47
Joined
Oct 29, 2018
Messages
21,478
Please let me know how to open subfolder from access form. I am able to open MyDocument but not able to open MyDocument /Cases/2020

Thanks for your help
Hi. Have you tried?

Application.FollowHyperlink "...\MyDocuments\Cases\2020"
 

vba_php

Forum Troll
Local time
Today, 11:47
Joined
Oct 6, 2019
Messages
2,880
hey, very nice guy. I might just employ that at some point. do you know if Allen Browne is still working? his homepage says he is not accepting new clients. he is from australia and it says he's in his 60's in Perth. and according to my scripts that I ran on his page, the following page was last updated 01/28/2018 19:13:35:


so obviously he is still doing something with it.
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Thank you everyone for your replies. Still I am not able to open the subfolders.

Here is my code I tried.

Shell "explorer.exe /" & """ & txtLink & """"", vbNormalFocus

The text box txtLink has the path (C:\Users\Joel\MyDocuments\Cases\2020). It opens only the MyDocument folder.

Thanks again for all your help.
 

vba_php

Forum Troll
Local time
Today, 11:47
Joined
Oct 6, 2019
Messages
2,880
You have already been told that the method you are using can be unreliable. Furthermore, there are many other alternatives that have been given to you already.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:47
Joined
Oct 29, 2018
Messages
21,478
Thank you everyone for your replies. Still I am not able to open the subfolders.

Here is my code I tried.

Shell "explorer.exe /" & """ & txtLink & """"", vbNormalFocus

The text box txtLink has the path (C:\Users\Joel\MyDocuments\Cases\2020). It opens only the MyDocument folder.

Thanks again for all your help.
Hi. Just checking, does the folder you're trying to open exist? What do you get with this code?

MsgBox Dir(txtLink, vbDirectory)

Sent from phone...
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Thank you for the reply. The message box was blank. No message.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:47
Joined
Oct 29, 2018
Messages
21,478
Thank you for the reply. The message box was blank. No message.
Hi. If the message box is blank, it might mean the folder doesn't exist. Can you confirm that? You can't open a folder until it's created.
 

vba_php

Forum Troll
Local time
Today, 11:47
Joined
Oct 6, 2019
Messages
2,880
Thank you, I tried, it says arguments not optional..
gsrajan,

this is another problem with VBA. sometimes you have to do this:
Code:
Application.FollowHyperlink ("...\MyDocuments\Cases\2020")
instead of what you tried:
Code:
Application.FollowHyperlink "...\MyDocuments\Cases\2020"
this happens with many functions and statements, and has for years.
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Hi. If the message box is blank, it might mean the folder doesn't exist. Can you confirm that? You can't open a folder until it's created.
Thank you for your reply. I checked, the folder is there.


Application.FollowHyperlink txtLink - it works for me now. Thanks so much for your reply.
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Thank you everyone, as you said Application.FollowHyperlink txtLink - works fine for me. Thanks again.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:47
Joined
Oct 29, 2018
Messages
21,478
Thank you for your reply. I checked, the folder is there.


Application.FollowHyperlink txtLink - it works for me now. Thanks so much for your reply.
Hi. Glad to hear you got it sorted now. Good luck with your project.
 

Users who are viewing this thread

Top Bottom