Solved

Error message for incorrect or missing hyperlink

Posted on 2017-05-19
3
34 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 125 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 55

Assisted Solution

by:Bill Prew
Bill Prew earned 125 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 21

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 250 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

687 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