Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

Error message for incorrect or missing hyperlink

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
SteveL13
Asked:
SteveL13
3 Solutions
 
NorieData ProcessorCommented:
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
 
Bill PrewCommented:
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
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now