Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

error handling in a vb code

Posted on 2010-09-02
6
219 Views
Last Modified: 2012-05-10
Hi,

The following code breaks when the file mstr.csv is not found. what I need to do is this. If the fille is not found then write to a log (timestamp + file (argmuent to deletefile) was not found) and continue to main task.

If the file is found then write to the log (with time stamp) and say successfully deleted and then continue to main task.  How do I achieve this

-------------------------------------------------

Public Sub Main()

deleteFile ("C:\mstr.csv")
Call MainTask

End Sub

Function deleteFile(sFilename As String)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.deleteFile (sFilename)
Set fso = Nothing
End Function
0
Comment
Question by:anshuma
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
PJBX earned 167 total points
ID: 33592471
Doesn't the FSO has a FileExists method? So:

Function deleteFile(sFilename As String)
Set fso = CreateObject("Scripting.FileSystemObject")
IF fso.FileExists(sfilename) then
   fso.deleteFile (sFilename)
Else
   'write to your log file
End if
Set fso = Nothing
End Function

0
 
LVL 6

Expert Comment

by:PJBX
ID: 33592542
Doesn't the FSO has a FileExists method? So:

Function deleteFile(sFilename As String)
Set fso = CreateObject("Scripting.FileSystemObject")
IF fso.FileExists(sfilename) then
   fso.deleteFile (sFilename)
   Set fso = Nothing
Else
   'write to your log file
End if
End Function
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33592571
Public Sub Main()
if deleteFile ("C:\mstr.csv") = -1 then
'write date() & "delete failed"
else
write  date() & "delete succesful"
end if
Call MainTask

End Sub

Function deleteFile(sFilename As String)
on error hoto hell
Set fso = CreateObject("Scripting.FileSystemObject")
fso.deleteFile (sFilename)
Set fso = Nothing
return 0
exit function
hell:
return -1
End Function
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Assisted Solution

by:Surone1
Surone1 earned 167 total points
ID: 33592585
Public Sub Main()
if deleteFile ("C:\mstr.csv") = -1 then
'debug.print date() & " delete failed"
else
'debug.print  date() & " delete succesful"
end if
Call MainTask

End Sub

Function deleteFile(sFilename As String)
on error goto hell
Set fso = CreateObject("Scripting.FileSystemObject")
fso.deleteFile (sFilename)
Set fso = Nothing
return 0
exit function
hell:
return -1
End Function
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33592600
turn off error handling (default): on error goto 0
ignore any erors and continue executing the code: on error resume next
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 166 total points
ID: 33593279
This is what you want:

Public Sub Main()
    deleteFile "C:\mstr.csv"
    Call MainTask
End Sub

Sub deleteFile(sFilename As String)
    Dim fso As Object
    Dim oStream As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oStream = fso.OpenTextFile("c:\mylog.log", 8, True) ' 8 - appending, 2 - writing
    If fso.FileExists(sFilename) Then
        fso.deleteFile sFilename, True  'force to delete
        oStream.WriteLine Format(Now, "yyyy-mm-dd hh:nn:ss") & ": File successfully deleted."
    Else
        oStream.WriteLine Format(Now, "yyyy-mm-dd hh:nn:ss") & ": File '" & sFilename & "' was not found."
    End If
    oStream.Close
    Set fso = Nothing
End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question