Solved

error handling in a vb code

Posted on 2010-09-02
6
221 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
message box in access 4 55
vb6 connector to SQL Server 2 42
TT Auto Dashboard 13 104
MS SQL Update query with connected table data 3 78
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

739 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