Solved

error handling in a vb code

Posted on 2010-09-02
6
216 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now