[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Error message for incorrect or missing hyperlink

Posted on 2017-05-19
3
Medium Priority
?
56 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 35

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 58

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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 …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

650 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