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
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.