Question : MS Access VBA Filecopy returns 70 - permission denied

I am writing an Access Application VBA function that copies files from one directory to another one that I have selected using the office filedialog box in msoFileDialogFolderPicker mode.  I am trying to use the filesystemobject FileCopy command, but get the error "70 - permission denied", even though I have full control permissions on the file and the folder to which I am copying.  My code is attached.  
Here is the contents of my immediate window when I execute the function:

?installemtscomprpt
File testprog.txt failed to copy to F:\PR09-023 - EMTS Adobe 9.0\EMTSCompRpt Installation Script\NewFolder.  Error is 70 - Permission denied.

Why am I getting this error message?

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
Option Compare Database
Option Explicit

Public fs As FileSystemObject, strOrigDir As String, strDestDir As String

Public Function InstallEMTSCompRpt() As String
    
    Set fs = New FileSystemObject
    strOrigDir = CurrentProject.Path & "\"
    strDestDir = fFolderDialog
    Debug.Print fCopyFile("testprog.txt")

End Function
Public Function fFolderDialog() As String

'Requires reference to Microsoft Office 10.0 Object Library.

   Dim fDialog As Office.FileDialog
   Dim varFile As Variant

   'Clear listbox contents.
   'Me.FileList.RowSource = ""

   'Set up the File Dialog.
   Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
   With fDialog
      'Allow user to select only one EMTSReports Database
      .AllowMultiSelect = False
            
      'Set the title of the dialog box.
      .Title = "Please select the EMTSReports Database to receive objects"

      'Clear out the current filters, and add our own.
      .Filters.Clear
      .InitialFileName = "\\okc-svr01\obishare$\Access Applications\EMTS\"
      .InitialView = msoFileDialogViewList
      
      'Show the dialog box. If the .Show method returns True, the
      'user picked at least one file. If the .Show method returns
      'False, the user clicked Cancel.
      If .Show = True Then
         'Loop through each file selected and add it to our list box.
         For Each varFile In .SelectedItems
            fFolderDialog = varFile
         Next
      Else
         fFolderDialog = ""
      End If
   End With
End Function
Public Function fCopyFile(strFileName As String) As String
Dim lstrSource As String
    
    On Error GoTo ErrfCopyFile
    
    lstrSource = strOrigDir & strFileName
    fs.CopyFile lstrSource, strDestDir
    fCopyFile = "File " & strFileName & " copied successfully to " & strDestDir & "."
    
ExitfCopyFile:
    Exit Function
    
ErrfCopyFile:
    fCopyFile = "File " & strFileName & " failed to copy to " & strDestDir & ".  "
    fCopyFile = fCopyFile & "Error is " & Err.Number & " - " & Err.Description & "."
    MsgBox fCopyFile, vbExclamation, "InstallEMTSCompRpt Error:"
    Resume ExitfCopyFile

End Function

Answer : MS Access VBA Filecopy returns 70 - permission denied

Guess what!  I have figured out the problem:

I had coded the fcopy function as follows:

Public Function fCopyFile(strFileName As String) As String
Dim lstrSource As String
   
    On Error GoTo ErrfCopyFile
   
    lstrSource = strOrigDir & strFileName
    fs.CopyFile lstrSource, strDestDir
    fCopyFile = "File " & strFileName & " copied successfully to " & strDestDir & "."
   
ExitfCopyFile:
    Exit Function

What I am basically doing here is equivalent to the copy SourceDirectory/file DestinationDirectory.  But unlike the command line command, FileSystemObject.CopyFile wants you to specify the destination directory AND FILENAME.  Otherwise it is apparently thinking you want to replace the folder with the file, which Windows prevents you from doing (Thank G-d).  When I specify the destination file name it works fine.  I have attached the corrected code.

It has not been a good day.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Public Function fCopyFile(strFileName As String) As String
Dim lstrSource As String, lstrDestination As String
    
    On Error GoTo ErrfCopyFile
    
    lstrSource = strOrigDir & strFileName
    lstrDestination = strDestDir & "\" & strFileName
    fs.CopyFile lstrSource, lstrDestination
    fCopyFile = "File " & strFileName & " copied successfully to " & strDestDir & "."
    
ExitfCopyFile:
    Exit Function
Random Solutions  
 
programming4us programming4us