D
deb
When I click a button on my form it calls the below function which opens a
dialog box and saves the link to a file.
If the user clicks cancel on the FileDialog, they get error 3314.
How can I edit the below code so if user cancels a msg wil display saying
file not selected or maybe just undo or delete the current record?
Sub GetFileName()
' Displays the Office File Open dialog to choose a file name for the current
record
Dim strFileName As String
Dim strDriveType As String
Dim strDriveLetter As String
Dim intResult As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Attachment"
'Note: After changing anything here, you need to quit Access and then
restart it:
.Filters.Add "Word Documents", "*.doc" 'Index = 1
.Filters.Add "Adobe Acrobat", "*.pdf" 'Index = 2
.Filters.Add "All Files", "*.*" 'Index = 3
.FilterIndex = 3 '<---Specifies default
index value to filter for.
.AllowMultiSelect = False
.InitialFileName = mstrInitialDir
intResult = .Show
If (intResult <> 0) Then
strFileName = Trim(.SelectedItems.Item(1))
' Make the selected folder the initial folder for the
FileDialogPicker
mstrInitialDir = Left$(strFileName, InStrRev(strFileName, "\"))
'Debug.Print "mstrInitialDir = " & mstrInitialDir
' Check path (local vs. shared folder)
If Left$(strFileName, 2) <> "\\" Then
strDriveLetter = Left$(strFileName, 2)
strDriveType = DriveType(strDriveLetter)
Select Case strDriveType
Case "Hard Disk"
If mblnWarnUser Then 'Warn user
intResult = MsgBox("The document that you selected
may not be available to" & vbCrLf _
& "other users, because you
selected a local hard drive." & vbCrLf & vbCrLf _
& "Would you like to add this
document?", vbCritical + vbYesNo, _
"Document Selected From Local
Hard Drive...")
End If
If intResult = vbNo Then
Me.Undo
Exit Sub
End If
Case "Network drive" 'This is the desired location, so
convert path to UNC
strFileName = ChangeMappedDriveToUNC(strFileName)
Case Else
MsgBox "There was a problem selecting from this
location." & vbCrLf _
& "You may have selected a folder in a removable
drive.", _
vbCritical, "Cannot Select From This Location..."
Exit Sub
End Select
End If
If mblnUseShortFileNames = True Then 'Convert selected path to
Short FileName
strFileName = GetShortName(strFileName)
End If
Me![ImagePath].Visible = True
Me![ImagePath].SetFocus
Me![ImagePath].Text = strFileName
Me.Requery
End If
End With
End Sub
dialog box and saves the link to a file.
If the user clicks cancel on the FileDialog, they get error 3314.
How can I edit the below code so if user cancels a msg wil display saying
file not selected or maybe just undo or delete the current record?
Sub GetFileName()
' Displays the Office File Open dialog to choose a file name for the current
record
Dim strFileName As String
Dim strDriveType As String
Dim strDriveLetter As String
Dim intResult As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Attachment"
'Note: After changing anything here, you need to quit Access and then
restart it:
.Filters.Add "Word Documents", "*.doc" 'Index = 1
.Filters.Add "Adobe Acrobat", "*.pdf" 'Index = 2
.Filters.Add "All Files", "*.*" 'Index = 3
.FilterIndex = 3 '<---Specifies default
index value to filter for.
.AllowMultiSelect = False
.InitialFileName = mstrInitialDir
intResult = .Show
If (intResult <> 0) Then
strFileName = Trim(.SelectedItems.Item(1))
' Make the selected folder the initial folder for the
FileDialogPicker
mstrInitialDir = Left$(strFileName, InStrRev(strFileName, "\"))
'Debug.Print "mstrInitialDir = " & mstrInitialDir
' Check path (local vs. shared folder)
If Left$(strFileName, 2) <> "\\" Then
strDriveLetter = Left$(strFileName, 2)
strDriveType = DriveType(strDriveLetter)
Select Case strDriveType
Case "Hard Disk"
If mblnWarnUser Then 'Warn user
intResult = MsgBox("The document that you selected
may not be available to" & vbCrLf _
& "other users, because you
selected a local hard drive." & vbCrLf & vbCrLf _
& "Would you like to add this
document?", vbCritical + vbYesNo, _
"Document Selected From Local
Hard Drive...")
End If
If intResult = vbNo Then
Me.Undo
Exit Sub
End If
Case "Network drive" 'This is the desired location, so
convert path to UNC
strFileName = ChangeMappedDriveToUNC(strFileName)
Case Else
MsgBox "There was a problem selecting from this
location." & vbCrLf _
& "You may have selected a folder in a removable
drive.", _
vbCritical, "Cannot Select From This Location..."
Exit Sub
End Select
End If
If mblnUseShortFileNames = True Then 'Convert selected path to
Short FileName
strFileName = GetShortName(strFileName)
End If
Me![ImagePath].Visible = True
Me![ImagePath].SetFocus
Me![ImagePath].Text = strFileName
Me.Requery
End If
End With
End Sub