?
Solved

Error message for incorrect or missing hyperlink

Posted on 2017-05-19
3
Medium Priority
?
49 Views
Last Modified: 2017-05-19
I have a form with a text field and a command button.  The text field contains the path to a document on a computer.  For example: C:\Users\Steve\Documents\CompanyName\123456.005.pdf

Then the user clicks on the command button to open that file.  The on click code for the command button is:

Private Sub cmdOpen_Click()

    Dim strFileName As String

    strFileName = Me.txtLocationPathandLink.Value
    
    Application.FollowHyperlink (strFileName)

End Sub

Open in new window


But if the file can't be found I need a custom message to appear like “File Not Found. Please contact the administrator.”

How can I change the code to make this message appear?
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Assisted Solution

by:Norie
Norie earned 500 total points
ID: 42142405
Try this.
Private Sub cmdOpen_Click()

    Dim strFileName As String

    strFileName = Me.txtLocationPathandLink.Value
    
    If Len(Dir(strFileName)) = 0 Then

        MsgBox "File Not Found. Please contact the administrator.”, vbExclamation, "File not found"

    End If

    Application.FollowHyperlink (strFileName)

End Sub

Open in new window

0
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 42142407
If you want to trap errors accessing the link, you can take this approach.  Replace MsgBox with whatever approach you want to take to showing the error to the user.

Private Sub cmdOpen_Click()

    Dim strFileName As String

    strFileName = Me.txtLocationPathandLink.Value
    
    On Error Resume Next
    Application.FollowHyperlink (strFileName)
    If Err.Number <> 0 Then
        MsgBox "ERROR opening file: " & strFileName & vbCrLf & "Error: " & Err.Number & " - " & Err.Description
    End If
    On Error GoTo 0

End Sub

Open in new window


»bp
0
 
LVL 22

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 1000 total points
ID: 42142570
Bill suggested skipping the error ... however not sure Access can know all the problems.

Norie's approach, to make sure file is there first, is a good start -- and then incorporate Bill's code ~

I would also add to the beginning (replace Bill's code for strFileName):
dim sPathFilename as string 'since it is Path and filename, not just filename
with Me.txtLocationPathandLink
   if isnull(.value) then exit sub
   sPathFilename =.value
end with

Open in new window

... and then use sPathFilename where you want the value in the rest of the procedure
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question