Thank RuralGuy. I was faced with this very problem three weeks ago and I ran accross his answer. So I am just passing it on.
PS: For my code to work I had to use the TRIM function. It took me a while to figure that one out.
Code:
dim FileExistsbol as boolean
dim stFileName as string
stFileName ="C:\Test File.txt"
stFileName =TRIM(stFileName)
FileExistsbol = Dir(stFileName) <> vbNullString
if FileExistsbol then Kill stFileName
Public Function FileExists(stFileName As String) As Boolean
Dim FileExistsbol As Boolean
stFileName = Trim(stFileName)
FileExistsbol = Dir(stFileName) <> vbNullString
FileExists = FileExistsbol
End Function
I came to this post while searching for code similar to the one Ripley mentioned. I save yearly data to a separate file through a function. This is done on first day of january. The file of previous year is saved as 'MyDB2006' (the code adds previous year to the file (Year(Date) -1).
I need a code which will check if 'MyDB2006' exists. I tried the following code
If Dir(D:\YearlyData\MyDB & Year(Date) -1) = 0 Then
'function to create file
Else
MsgBox "File exists"
End If