Question

I have an MS Access VBA procedure inside a module that has a for each loop on files in a particular folder.

Inside the loop, I create a new file in a separate folder that is then filled with cleaned data from the file in the loop. I import the new file's data into a SQL Server 2005 database. Upon successful import, the script deletes the cleaned file and attempts to move (or copy/delete) the file in the loop to an archive subfolder in the same directory.

The file looping works fine on all files in the looped folder... except for the last file. That's when the Permission Denied error pops up.

The error message, under the above-mentioned circumstance, always happens on one of the following (I've tried several, similar commands):

fso.MoveFile
fso.DeleteFile (just after copy)
f.Move
f.Delete (just after copy)
Name origin As destination

Here is my code:

Dim fso As New Scripting.FileSystemObject
Dim f As file
Dim processingFiles As Files

If fso.FolderExists(incomingPath) Then
    Set processingFiles = fso.GetFolder(incomingPath).Files
End If

For Each f In processingFiles

    /*this is where the create a new file and clean it part runs - works fine*/

    If fso.FileExists(archivePathFile) Then
        Kill archivePathFile
    End If

    If fso.FileExists(tempPath & "\cleaned_processing_file.txt") Then
        Kill tempPath & "\clean_processing_file.txt"
    End If


    f.Move archivePathFile   '<------------- Permission Denied, last file in folder
    Debug.Print f.Name & " is now in " & incomingPath & "\Archive"

    'f.Copy archivePathFile, True 
    'f.Delete True '<----------------------- Permission Denied, last file

    'Name origPathFile As archivePathFile '< Path/File access error, last file

Next '<--- For Each Loop
Was it helpful?

Solution

I'm posting this as an "answer" so the code will show up, but you (obviously) don't have to accept it...

I just tested the minimal case...

Sub move_to_foo()
Dim fso As New FileSystemObject
Dim processingFiles As Files, f As File
Set processingFiles = fso.GetFolder("C:\__tmp").Files
For Each f In processingFiles
    Debug.Print "Moving """ & f.Name & """..."
    f.Move "C:\__tmp\foo\" & f.Name
Next
End Sub

...and it does not fail on the last file in the folder, so your problem has to be something specific to your code. If you choose to edit your question and provide more details then I'll be happy to help, but right now your question -- as stated -- has no answer.

OTHER TIPS

I had the exact same problem as you. All files would be moved from the source directory to the target directly except for the last one, which would cause "Permission Denied" to be displayed. This would happen if there were 2 files or 30 files. I was copying the data from the files to a SQL database before the files were moved as well. The solution is to set the objects to "Nothing" after they are used to parse the files and insert the SQL data.

For example, after the SQL script data is inserted, the files can be moved. Free up the object resources:

Set objFSO = Nothing
Set objTextFile = Nothing
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top