How do I use FileExists?
How do I use FileExists?
(OP)
thread766-141175: Checking if a file exists.
Working off of the thread above, I am trying to have VBA code
1. Open a Select Query, on a Hyperlink field, based on a list box selection ("txtWellID")
2. Check if a PDF file exists for the item in "txtWellID" then
a. Open the hyperlink, or else
b. Display a MsgBox and close the query
I am a bit of a Virgin VBA coder, and have never used the "FileExists" method, but believe it to be what I need to accomplish this task. Below is a rough sample of my attempt. It was working to open the hyperlink before adding "FileExists" method, so I know that portion of code was working; but I did change around to add "FileExists". Can anyone modify for me please?
Working off of the thread above, I am trying to have VBA code
1. Open a Select Query, on a Hyperlink field, based on a list box selection ("txtWellID")
2. Check if a PDF file exists for the item in "txtWellID" then
a. Open the hyperlink, or else
b. Display a MsgBox and close the query
I am a bit of a Virgin VBA coder, and have never used the "FileExists" method, but believe it to be what I need to accomplish this task. Below is a rough sample of my attempt. It was working to open the hyperlink before adding "FileExists" method, so I know that portion of code was working; but I did change around to add "FileExists". Can anyone modify for me please?
CODE
Private Sub cmdOK_Click()
'Opens Groundwater Trend Charts, based on hyperlinks in the table "Well_info"
Dim qryTrendChartLinks As Hyperlink
Dim txtWellID As String
txtWellID = Forms!frmWellTrendChartsDialog!txtWellID
On Error GoTo ErrorHandler
Set FSO = New FileSystemOBject
DoCmd.OpenQuery ("qryTrendChartLinks")
If FSO.FileExists("D:\Trend Charts" & "txtWellID" & ".pdf") Then
DoCmd.RunCommand acCmdOpenHyperlink
DoCmd.Close acQuery, "qryTrendChartLinks", acSaveNo
Else
'If Not ("qryTrendChartLinks") Like "*" Then
MsgBox ("Could not find a report")
ErrorHandler: MsgBox ("Cannot find Trend Chart")
DoCmd.Close acQuery, "qryTrendChartLinks", acSaveNo
End If
End Sub
'Opens Groundwater Trend Charts, based on hyperlinks in the table "Well_info"
Dim qryTrendChartLinks As Hyperlink
Dim txtWellID As String
txtWellID = Forms!frmWellTrendChartsDialog!txtWellID
On Error GoTo ErrorHandler
Set FSO = New FileSystemOBject
DoCmd.OpenQuery ("qryTrendChartLinks")
If FSO.FileExists("D:\Trend Charts" & "txtWellID" & ".pdf") Then
DoCmd.RunCommand acCmdOpenHyperlink
DoCmd.Close acQuery, "qryTrendChartLinks", acSaveNo
Else
'If Not ("qryTrendChartLinks") Like "*" Then
MsgBox ("Could not find a report")
ErrorHandler: MsgBox ("Cannot find Trend Chart")
DoCmd.Close acQuery, "qryTrendChartLinks", acSaveNo
End If
End Sub





RE: How do I use FileExists?
In quotes = literal string
No quotes = variable
RE: How do I use FileExists?
CODE
RE: How do I use FileExists?
TTFN
FAQ731-376: Eng-Tips.com Forum Policies
Chinese prisoner wins Nobel Peace Prize
RE: How do I use FileExists?
CODE
Set FSO = CreateObject("Scripting.FileSystemObject")
RE: How do I use FileExists?
CODE
'Opens Groundwater Trend Charts, first checking if PDF exists
Dim strDocument As String
Dim txtWellID As String
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
txtWellID = Forms!frmWellTrendChartsDialog!txtWellID
strDocument = ("D:\Charts\" & txtWellID & ".pdf")
If FSO.FileExists("D:\Charts\" & txtWellID & ".pdf") Then
Application.FollowHyperlink strDocument
Else
MsgBox ("Could not find a chart for the specified well")
End If
End Sub