filedialog of user cancels

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
 
D

Dirk Goldgar

deb said:
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


I don't see any reason why that code would fail if the user clicks Cancel on
the file dialog. I tried copying it into a module of my own (commenting out
calls to unavailable functions), and there was no problem when I clicked the
dialog's Cancel button. Are you sure that's what is happening? Have you
tried setting a breakpoint on the call to .Show and stepping through to see
what happens?
 
D

deb

I tried again and it does not error now.

But
Is there a way to undo or delete the record is user cancels?

Thank you!!
--
deb


Dirk Goldgar said:
deb said:
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


I don't see any reason why that code would fail if the user clicks Cancel on
the file dialog. I tried copying it into a module of my own (commenting out
calls to unavailable functions), and there was no problem when I clicked the
dialog's Cancel button. Are you sure that's what is happening? Have you
tried setting a breakpoint on the call to .Show and stepping through to see
what happens?

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 
D

Dirk Goldgar

deb said:
I tried again and it does not error now.

My guess is that the former error was due to factors external to the
procedure you posted.
But
Is there a way to undo or delete the record is user cancels?

Well, you know if the user cancelled the dialog, because intResult (set to
the return code from the .Show method) will be 0. So you could change this
line:
If (intResult <> 0) Then

.... to this:

If intResult = 0 Then
' The user cancelled the dialog.
' Undo the current record on the form.

Me.Undo

' Note: if you wanted to delete the record, you could do
' something like this:
'
' If Me.Dirty Then Me.Undo
' If Not Me.NewRecord Then
' RunCommand acCmdDeleteRecord
' End If

Else
' The user selected a file.

' ... rest of original code goes here ...
 
D

deb

-- intResult is yes no when user is warned that the file is on the C: do
you want to continue? Y N

The undo is already there if user selects No.

I need to undo if user cancels the file dialog.

Thanks in advance

deb
 
D

deb

My bad.

I edited the wrong line.

I substituted your code and put an else at the end and it works perfecrt!!!


Thank you!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top