File Not Found

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

I have this statement that gives me the error "File Not Found"
Is there a way that I can code this if I get an error nothing happens,
no warning or anything I just go to the next line of code.

Kill "C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB"

Thnks
DS
 
Yes. Place an error handler in your code (you should be doing this anyways).

Something like

Function DelFile()
On Error GoTo DelFile_Error

Kill "C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB"

DelFile_Error:
If Err.Number <> 0 Then
MsgBox "MS Access has generated the following error" & vbCrLf &
vbCrLf & "Error Number: " & _
Err.Number & vbCrLf & "Error Description: " & Err.Description,
vbCritical, "An Error has Occured!"
Elseif err.number = xxxx Then

End If
Exit Function
End Function


replace xxx by the err.number that is raised when the file doesn't exist.
you could also simply check for the file using the DIR() function and only
delete the file if it actually exists.
 
The Format function needs to be outside of the quotes:

Dim strFile As String

strFile = "C:\Proserv\BU\" & Format(Date, "yyyymmdd") & "-8.MDB"

If Len(Dir(strFile)) > 0 Then
Kill strFile
FileCopy "C:\PROSERV\DB\Restore.mdb", _
"C:\PROSERV\BU\" & Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") &
".mdb"
End If

Of course, I don't pretend to understand what you're trying to do. What
difference does it make if the first file exists?
 
What's the - 8 for? Format returns a string: you can't subtract a number
from a string.
 
DS said:
I have this statement that gives me the error "File Not Found"
Is there a way that I can code this if I get an error nothing happens,
no warning or anything I just go to the next line of code.

Kill "C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB"

Thnks
DS
I tried this but it doesn't work...I get type mismatch.

If Dir("C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB""") >=1 Then
Kill "C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB"
FileCopy "C:\PROSERV\DB\Restore.mdb", "C:\PROSERV\BU\" &
Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") & ".mdb"
Else:
End If

This also doesn't work....

If Dir("C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB""") <> "" Then
End If
Else:
Kill "C:\Proserv\BU\Format(Date, ""yyyymmdd"")-8 & "".MDB"
FileCopy "C:\PROSERV\DB\Restore.mdb", "C:\PROSERV\BU\" &
Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") & ".mdb"
End If


If the file is there I simply want to erase it, if it is not then I want
to go to the next step which is to kill it!

Thanks
DS
 
DS said:
Thanks Douglas,
I have a series of Backup files that I run for 7 days, after the 7th day
it deletes anything older than 7 days.

Well so far I no longer get an error if the file doesn't exist. Byt it's
not deleteing the old file. Could it be that the formats are different?
It should be in this format mmddyy.

It likely is. You're explicitly telling it to look for files that have the
date in yyyymmdd format. If they're in mmddyy format, then it's no surprise
that it's not finding them!

Your code isn't deleting anything older than 7 days. If you want it to
delete a single file that's exactly 8 days old, you'd need:

Dim strFile As String

strFile = "C:\Proserv\BU\" & Format(Date - 8, "mmddyy") & ".MDB"

If Len(Dir(strFile)) > 0 Then
Kill strFile
End If

FileCopy "C:\PROSERV\DB\Restore.mdb", _
"C:\PROSERV\BU\" & Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") & ".mdb"

If you wanted to check for files that were, say, 8 to 14 days old, you'd
need a loop:

Din intLoop As Integer
Dim strFile As String

For intLoop = 8 To 14
strFile = "C:\Proserv\BU\" & Format(Date - intLoop, "mmddyy") & ".MDB"
If Len(Dir(strFile)) > 0 Then
Kill strFile
End If
Next intLoop

FileCopy "C:\PROSERV\DB\Restore.mdb", _
"C:\PROSERV\BU\" & Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") & ".mdb"



Note that I've moved the FileCopy statement outside of the If loop: the way
you had it, you'd only be copying the file if an 8 day old file existed.
 
I also tried this...

Me.TxtDir = DCount(Dir("C:\Proserv\BU\" & Format(Date, "yyyymmdd")) - 8
& ".MDB")

Doesn't do it either!
DS
 
Douglas said:
The Format function needs to be outside of the quotes:

Dim strFile As String

strFile = "C:\Proserv\BU\" & Format(Date, "yyyymmdd") & "-8.MDB"

If Len(Dir(strFile)) > 0 Then
Kill strFile
FileCopy "C:\PROSERV\DB\Restore.mdb", _
"C:\PROSERV\BU\" & Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") &
".mdb"
End If

Of course, I don't pretend to understand what you're trying to do. What
difference does it make if the first file exists?
Thanks Douglas,
I have a series of Backup files that I run for 7 days, after the 7th day
it deletes anything older than 7 days.

Well so far I no longer get an error if the file doesn't exist. Byt
it's not deleteing the old file. Could it be that the formats are
different? It should be in this format mmddyy.
Thanks
DS
 
Douglas said:
The Format function needs to be outside of the quotes:

Dim strFile As String

strFile = "C:\Proserv\BU\" & Format(Date, "yyyymmdd") & "-8.MDB"

If Len(Dir(strFile)) > 0 Then
Kill strFile
FileCopy "C:\PROSERV\DB\Restore.mdb", _
"C:\PROSERV\BU\" & Format(Forms!frmFXEOD!TxtBizDate, "mmddyy") &
".mdb"
End If

Of course, I don't pretend to understand what you're trying to do. What
difference does it make if the first file exists?
Got It Douglas,
Here it is..
Dim strFile As String
strFile = "C:\Proserv\BU\" & Format(Date, "yyyymmdd") - 8 & ".MDB"
If Len(Dir(strFile)) > 0 Then
Kill strFile
FileCopy "C:\PROSERV\DB\Restore.mdb", "C:\PROSERV\BU\" &
Format(Forms!frmFXEOD!TxtBizDate, "yyyymmdd") & ".mdb"
End If

The -8 was in th ewrong spot.
I appreciate all of your help!
Thanks
DS
 
Glad you got it working. However, you might want to reconsider the statement
"There is never more than 8 files in the folder and only one ever needs
deleting.". What happens if something happens and that's not true?
 
Thanks Douglas,
This is what I've ended up with...
It's Copying and Deleteing so I guess it's working!

Dim strFile As String
strFile = "C:\Proserv\BU\" & Format(Date - 8, "yyyymmdd") & ".MDB"
If Len(Dir(strFile)) > 0 Then
Kill strFile
FileCopy "C:\PROSERV\DB\Restore.mdb", "C:\PROSERV\BU\" &
Format(Forms!frmFXEOD!TxtBizDate, "yyyymmdd") & ".mdb"
End If

There is never more than 8 files in the folder and only one ever needs
deleting. So I don't need to loop. Also I stayed with the YYYYMMDD
format because I thought that it made the most sense when archieving.

Once again, many thanks.
DS
 
Douglas said:
Glad you got it working. However, you might want to reconsider the statement
"There is never more than 8 files in the folder and only one ever needs
deleting.". What happens if something happens and that's not true?
Never thought about that. At worst I guess I would start accumulating
older mdb's. Is there a solution to this?
Thanks
DS
 
Other than the code I've already suggested? Sure. You could use the Dir
function to loop through the folder, checking for each file that there's and
determine which of them you need to delete.
 
Douglas said:
Other than the code I've already suggested? Sure. You could use the Dir
function to loop through the folder, checking for each file that there's and
determine which of them you need to delete.
Thanks Douglas,
I'll consider this after I get out of this other problem!
Thanks
DS
 
Back
Top