Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling in Loop and Resume 1

Status
Not open for further replies.

mjjks

Programmer
Jun 22, 2005
138
US

Hi All,
I'm strugling to develop an error handling solution and need some advice.
VB6 app I've been asked to enhance have a module that basically gets a recordset from DB, loops and adds data to a treeview nodes.
Backend data is not very clean as it comes from differents sources (import, 3rd party vendor apps). Errors are handled but it's an issue as this procedure is also called as a refresh through timer, so user gets pop-up messages quite often.

I've been asked to remove that and just add all errors to a new "Errors" node. Code needs to resume execution until all records are processed.

I added the following structure, but I get only first record added during my test and then it stops. Any suggestions? Thanks.

Code:
'-- Get recordset here

Do Until rs.EOF
 [highlight #FF99FF]On Error GoTo ErrAddToTree[/highlight]

 '-- add regular records to treeview nodes here

 '-- raise an error for testing
 Err.raise 16,"Test","Test"

 '-- label to resume execution
 [highlight]ResumeExecution:[/highlight]

rs.MoveNext
Loop

Exit Sub

'-- add record's data to "Errors" node and resume execution
[highlight #FF99FF]ErrAddToTree:[/highlight]
  Set nodTemp = tvwIncd.Nodes.Add("Errors", tvwChild,intNextAlert, strErrTemp, _
    "INCD", "INCDSELECTED")
    nodTemp.Tag = lngErrAlertID
    [highlight]GoTo ResumeExecution[/highlight]

 
I'm not sure about the rest of your code but on finishing an error handler it is usual to use Resume not Goto.

Resume Clears the value of Err.Number back to zero so that the Err object it is ready to catch the next error. Goto will not do that.

HTH Hugh
 
Thanks Hugh.
I replaced my "GoTo ResumeExecution" label with "Resume Next" and it worked just fine.
 
Here's a little demo I use in class:

Private Sub Command1_Click()
Dim x As Double
On Error GoTo ErrHandle
InputLine:
x = InputBox("Enter a number:")
MsgBox "The reciprocal of " & x & " is " & CStr(1 / CDbl(x))
Exit Sub
ErrHandle:
Select Case Err.Number
Case 11 'Div by 0
MsgBox "Enter a number other than zero"
Resume InputLine
Case 13 'Type mismatch
MsgBox "Enter a numeric value"
Resume
End Select
End Sub

The Type Mismatch error occurs on the InputBox line, so you can just repeat the line that threw the error. However, the Div by 0 error occurs on the MsgBox line, so you have to specify the line to resume on.

So: resume next means ignore the line that caused the error (presumably because you ran some alternative code routine in the error handling context), resume means to re-execute the offending line of code and resume tagline (or a line number works as well) is for specifying exactly where to resume. In the case of mjjks's code, resume next and resume resumeexecution are equivalent.

HTH

Bob


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top