Solved

Error message for incorrect or missing hyperlink

Posted on 2017-05-19
3
24 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
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 54

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
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 20

Accepted Solution

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

737 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