Solved

error handling in a vb code

Posted on 2010-09-02
6
217 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now