I am sure this is posted somewhere but I could not find it.
What I am wanting to do is use the code below to determine if a file exists give the user an option to open the file. It is working like I would expect it to when a body no. has an associated file.
The issue arises when a body no does not have an associated file. It still pops up the msgbox saying a file exists but when I click yes to view it ends the sub.
What should happen is if a body no has no associated file show a msgbox saying no file exists, in this case it is "Keep Working" and the user can then carry on to something else.
I'm sure it is something simple like msgbox code placement but any help will be appreciated.
What I am wanting to do is use the code below to determine if a file exists give the user an option to open the file. It is working like I would expect it to when a body no. has an associated file.
The issue arises when a body no does not have an associated file. It still pops up the msgbox saying a file exists but when I click yes to view it ends the sub.
What should happen is if a body no has no associated file show a msgbox saying no file exists, in this case it is "Keep Working" and the user can then carry on to something else.
I'm sure it is something simple like msgbox code placement but any help will be appreciated.
Code:
Private Sub cmdCheckThreePanel_Click()
Dim sMyPath As FileDialog
Dim sPath As Variant
Dim strFolderPath As String
Dim strMessage As String
Dim strTitle As String
strMessage = "A Three Panel exists for this Body No." & Chr(13) & Chr(10) & _
"Would you like to view this file now?"
strTitle = "Three Panel Exists"
strFolderPath = "C:\Users\cole.stratton\Documents\Procurement\Database Folders\Three Panels\" & Me.Supplier & "\"
Response = MsgBox(strMessage, vbYesNo, strTitle)
Set sMyPath = Application.FileDialog(msoFileDialogFilePicker)
Debug.Print "Looking for: " & strFolderPath
strFile = Dir(strFolderPath & "*" & Me.BodyNo & "*")
'Add an 'AND' statement to the If function to check body number in filename
'If BodyNo does NOT have a file, return a no file exists.
If (strFile <> "") Then
If Response = vbYes Then
With sMyPath
.AllowMultiSelect = False
.InitialFileName = "C:\Users\cole.stratton\Documents\Procurement\Database Folders\Three Panels\" & Me.Supplier
.Title = "Three Panel Check"
.Filters.Add "All Files", "*.*"
.Show
End With
Else
MsgBox "Action Cancelled"
End If
Else
MsgBox "Keep working"
End If
End Sub