'Open a file from network drive by clicking on a link - in Microsoft Access form

I have the below code to open a file location FOLDER, however I would like to open the file itself instead of folder location.

Can someone suggest what should I change in the code.

Private Sub File_locationButton_Click()

    Dim filePath
    filePath = File_Location 

    Shell "C:\WINDOWS\explorer.exe """ & filePath & "", vbNormalFocus

End Sub


Solution 1:[1]

You can use ShellExecute, so:

Private Sub File_locationButton_Click()

    Dim filePath
    filePath = File_Location 

    OpenDocumentFile filePath

End Sub

which calls:

Option Compare Database
Option Explicit

' API declarations for OpenDocumentFile.
' Documentation:
'   https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
'
Private Declare PtrSafe Function ShellExecute 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

Private Declare PtrSafe Function GetDesktopWindow Lib "USER32" () _
    As Long
    
' ShowWindow constants (selection).
' Documentation:
'   https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
'
Private Const SwShowNormal      As Long = 1
Private Const SwShowMinimized   As Long = 2
Private Const SwShowMaximized   As Long = 3

    
' Open a document file using its default viewer application.
' Optionally, the document can be opened minimised or maximised.
'
' Returns True if success, False if not.
' Will not raise an error if the path or file is not found.
'
' 2022-03-02. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function OpenDocumentFile( _
    ByVal File As String, _
    Optional ShowCommand As Long = SwShowNormal) _
    As Boolean

    Const OperationOpen     As String = "open"
    Const MinimumSuccess    As Long = 32
    ' Shall not have a value for opening a document.
    Const Parameters        As String = ""
    
    Dim Handle      As Long
    Dim Directory   As String
    Dim Instance    As Long
    Dim Success     As Boolean
    
    Handle = GetDesktopWindow
    Directory = Environ("Temp")

    Instance = ShellExecute(Handle, OperationOpen, File, Parameters, Directory, ShowCommand)
    ' If the function succeeds, it returns a value greater than MinimumSuccess.
    Success = (Instance > MinimumSuccess)
    
    OpenDocumentFile = Success

End Function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gustav