Click to See Complete Forum and Search --> : Need some help with error handling
sriky
February 21st, 2000, 02:39 PM
I have an application that opens several files.
It has to check if that file resides in the specified directory.
Also it has to check if there is any data in the file.
If not, it has to rise approprite messages. How can I accomplish this?
thanks for any suggestions
Kyle Burns
February 21st, 2000, 02:53 PM
One solution would be to use the Dir Function. This function returns the filename if the file exists, and an empty string if it does not.
Dim sFileName as string
sFileName = Dir("C:\Windows\Desktop\MyFile.txt")
If sFileName = vbNullString then
'The file does not exist, raise an error
Err.Raise ERROR_NO_FILE 'Define this error number
else
'Open the file
'Depending on your choice for access
'Check for EOF and BOF and set a boolean
'to indicate no data
If bNoData then
Err.Raise ERROR_NO_DATA
else
'Read and process the file
End If
'Close the file
End If
Johnny101
February 21st, 2000, 02:59 PM
I would suggest using the FileSystemObject:
Sub Main()
'set a reference to the Microsoft Scripting Runtime
Dim fso as scripting.filesystemobject
dim filer as scripting.textstream
set fso = new scripting.filesystemobject
if fso.fileexists("C:\yourfile.txt") then
set filer = fso.opentextfile("C:\yourfile.txt",ForReading,false)
else
msgbox "File does not exist."
goto ExitRoute
end if
if filer.atendofstream then
msgbox "File is empty."
end if
ExitRoute:
on error resume next
filer.close
set filer = nothing
set fso = nothing
End Sub
That should do it for you.
John
John Pirkey
MCSD
www.ShallowWaterSystems.com
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.