Solved

error handling in a vb code

Posted on 2010-09-02
6
218 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

770 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