PDA

View Full Version : Solved: How to delete a file when KILL doesn't work?



AirCooledNut
09-21-2012, 08:28 AM
I have a routine that is supposed to delete a file on a network folder. Everytime it executes the KILL command I get a "File not found" error and yet the file is there! :dunno

This is the command I'm using:
Kill !pathname 'Delete the electronic file! which is found here:
...
'Now delete the files!
SysCmd acSysCmdSetStatus, "Deleting electronic files..."
On Error GoTo Err_Execute 'Use our error handling
bError = False 'Set default
Set db = CurrentDb 'Get our [current] database
Set rstErrorTable = db.OpenRecordset("tblDestroy_Electronic_Err") 'Get the table for capturing errors
Set rstFormRecords = Me.FilesToDestroy.Form.Recordset 'Get the records from the subform
With rstFormRecords
.MoveFirst 'Make sure we start at the beginning
Stop 'Testing
Do While Not .EOF 'Loop thru all the records until we hit the end
bErrorFlag = False 'This flag is set to TRUE in the error handler (indicates an error occurred)
Kill !pathname 'Delete the electronic file!
'Update DestroyedDate, DestroyedBy, Witness, and ScanExists fields in tblMasterScans but make no updates to tbl_Project_Locations.
If Not bErrorFlag Then
db.Execute "UPDATE tblMasterScans SET DestroyedDate = #" & Me.txtDestroyDate & "#" _
& ", DestroyedBy = '" & Me.txtDestroyedBy & "'" _
& ", Witness = '" & Me.txtDestroyWitness & "'" _
& ", ScanExists = 0" _
& " WHERE Project = '" & !PROJECTID & "' AND Path = '" & !pathname & "'" _
& ";", dbFailOnError
End If
.MoveNext 'Advance to the next record
Loop
End With
On Error GoTo 0 'Resume normal error handling
rstErrorTable.Close
Set rstFormRecords = Nothing
Set rstErrorTable = Nothing
Set db = Nothing
SysCmd acSysCmdSetStatus, " "
... The value for !PathName is
X:\DocumentRetention\ProjectScans\aTemp\toby.txt
and it is a string (Text data type in the table). I do have access to the network drive and can add/edit/delete files there.

What am I doing wrong? Is there another way to delete a file instead of KILL()? :help

AirCooledNut
09-21-2012, 09:22 AM
Searching got me these two URLs to try another method:
http://visualbasic.ittoolbox.com/groups/technical-functional/vb-access-l/using-the-filesystemobject-in-access-2003-vba-2539018 and http://stackoverflow.com/questions/67835/deleting-a-file-in-vba
so I tried this in place of the KILL command and it, too, produces the same error:

Dim fso As New Scripting.FileSystemObject
...
fso.DeleteFile (!Pathname), True 'True or False doesn't seem to matter
...


What could be causing Access to not "see" the file?

AirCooledNut
09-21-2012, 09:56 AM
:omg2:
Nevermind, stupid user error by me :o:

The filepath in the test record used "...aTemp..." but I named the test directory "...aTest...". Once I changed the directory name all worked as expected. Oh boy.

What got me here was trying to fso.MoveFile the file using the file system object. When I got the error that the Path doesn't exist I studied the two paths character by character :reading: and that's when I saw the discrepancy :doh: