MS Access VBA – Open a File

It can be very useful at times to store the paths of files within a database such as word document, excel spredsheets, etc. Most often it is then necessary to provide the user a method of opening these files without needing to personally navigating to each file themselves. The following line of code will open the given file in the default application associated with it.

Application.FollowHyperlink Method

Application.FollowHyperlink "FullPath&FileName"Example:
 
Application.FollowHyperlink "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg"

You can use the Application.FollowHyperlink can also be used to open a folder (not just files)

 

Custom Procedure Method

Another very good alternative is to use the ExecuteFile sub courtesy of Graham Seach (Access MVP). A nice feature is that you can also choose to print the file and control the appearance of the windowstyle of the given application.

'Source: http://www.pacificdb.com.au/MVP/Code/ExeFile.htm
Public Const SW_HIDE = 0
Public Const SW_MINIMIZE = 6
Public Const SW_RESTORE = 9
Public Const SW_SHOW = 5
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOWNORMAL = 1
 
Public Declare 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
 
Public Sub ExecuteFile(sFileName As String, sAction As String)
    Dim vReturn As Long
    'sAction can be either "Open" or "Print".
    
    If ShellExecute(Access.hWndAccessApp, sAction, sFileName, vbNullString, "", SW_SHOWNORMAL) < 33 Then
        DoCmd.Beep
        MsgBox "File not found."
    End If
End Sub

4 Comments to “MS Access VBA – Open a File”

  1. If i needed to open a specific file based an a staff id. Would this app follow hyperlink work for me? If i stored the full path & file name in the staff table, how could i code the command button to act on it? Many thanks

    • You should be able to modify the code to suit your needs.

      For instance, if you were on a form displaying a specific staff member’s id in a control named ‘Responsible’, then you could call the value as a variable in the FollowHyperlink method. Something along the lines of:

      Dim sBasePath As String
      Dim sBaseFileName As String
       
      sBasePath = "C:\...\...\"
      sBaseFileName = "Staff_"
       
      Application.FollowHyperlink sBasePath & sBaseFileName & Me.Responsible & ".xls"

      As you mentioned, you could also build a table and then pull the values from there using DLookup or a query of some sorts. But the sort answer is yes, the FollowHyperlink method can be used with variable filename.

  2. Thanks for replying to me. Is the base path/file name the database address or the folder where my JPG’s are stored? How would it pick up the Staff ID Number to pass over to the hyperlink? Many Thanks

    • sBasePath is the full path to where you house the document(s) you wish to hyperlink

      As for picking up your Staff ID Number, this would be done by concatenating the value from the active form (as per the previous example), such as: Me.Responsible or Me.StaffId.

Leave a Reply









Spam protection by WP Captcha-Free