Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / COM / June 2007



Tip: Looking for answers? Try searching our database.

Handling success errors in a For Loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim - 19 Jun 2007 21:04 GMT
What modifications are needed to get the desired results?

Thanks for all help.
Tim

'Results
'1st iteration: - Debug Window: ' -2147209159 - Test Prob: 1'
'2nd iteration: - Run-time error ' -2147209159 ()' & 'Test Prob: 2'

'Desired Results
'1st iteration: - Debug Window: ' -2147209159 - Test Prob: 1'
'2nd iteration: - Debug Window: ' -2147209159 - Test Prob: 2'
'3rd iteration: - Debug Window: ' -2147209159 - Test Prob: 3'
'4th iteration: - Debug Window: ' -2147209159 - Test Prob: 4'
'5th iteration: - Debug Window: ' -2147209159 - Test Prob: 5'

Sub Main()
Dim i As Integer
On Error GoTo ErrTrap
For i = 1 To 5
 Test i:=i
ErrTrap:
 If Err.Number <> 0 Then
   Debug.Print Err.Number & " - " & Err.Description
   Err.Clear
 Else
   Debug.Print Format(i, "0. ") & " - Ok"
 End If
Next
End Sub

Sub Test(i As Integer)
Err.Raise vbObjectError + 12345, , "Test Prob: " & CStr(i)
End Sub
MikeD - 19 Jun 2007 23:01 GMT
> What modifications are needed to get the desired results?
>
[quoted text clipped - 30 lines]
> Err.Raise vbObjectError + 12345, , "Test Prob: " & CStr(i)
> End Sub

Well, you weren't really too clear on a couple things, but I *think* what
you want is this:

Sub Main()
Dim i As Integer
On Error GoTo ErrTrap
For i = 1 To 5
 Test i:=i
Next
Exit Sub

ErrTrap:
 Debug.Print Err.Number & " - " & Err.Description
 Resume Next
End Sub

You should not be entering into your error handler routine if there's no
error.

Alternatively, I suppose you could do this:

Sub Main()
Dim i As Integer
On Error Resume Next

For i = 1 To 5
 Test i:=i
 If Err.Number <> 0 Then
   Debug.Print Err.Number & " - " & Err.Description
   Err.Clear
 Else
   Debug.Print Format(i, "0. ") & " - Ok"
 End If
Next
End Sub

IOW, what you were doing is kind of mixing and matching 2 different error
handling techniques.

Signature

Mike
Microsoft MVP Visual Basic

Tim - 20 Jun 2007 15:33 GMT
> > What modifications are needed to get the desired results?
>
[quoted text clipped - 75 lines]
>
> - Show quoted text -

I modified it slightly to achieve a slightly different result.
Thanks for the help.

'New Desired Results
'Test Prob: 1
'Test Prob: 2
'Test Prob: 3
'Test Prob: 4
'Inside Test: 5
'No error. Iteration: 5

Sub Main()
Dim i As Integer, blnErr As Boolean
blnErr = False
On Error GoTo ErrTrap
For i = 1 To 5
 blnErr = False
 Test i:=i
 If blnErr = False Then  'This needs to occur if No Err
   Debug.Print "No error. Iteration: " & CStr(i)
 End If
Next
Exit Sub
ErrTrap:
blnErr = True
Debug.Print Err.Description
Resume Next
End Sub

Sub Test(i As Integer)
If i < 5 Then
 Err.Raise vbObjectError + 12345, , "Test Prob: " & CStr(i)
End If
Debug.Print "Inside Test: " & CStr(i)
End Sub
Sinna - 21 Jun 2007 07:25 GMT
>>> What modifications are needed to get the desired results?
>>> Thanks for all help.
[quoted text clipped - 105 lines]
> Debug.Print "Inside Test: " & CStr(i)
> End Sub

Just a side note (on your programming):
    If blnErr = False Then
is more easily read as
    If Not blnErr Then
otherwise VB interprets it this way:
if blnErr is False:
    If False = False Then
        False equals False so the if returns True
else
    If True = False Then
        True doesn't equal False so if returns False

Sinna
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.