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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On Error Resume Next - Need MsgBox control. 2

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
I am using VBScript to read through a bunch of text files and load the records into a SQL server table. If my VB Script doesn't have an "On Error Resume Next" statement, one of the records generates an error message that says Incorrect Date Format. If I put in the "On Error..." statement, the script blows through the error and I don't see it at all. If I were in VBA, I would use "On Error GoTo ErrHdlr", but VBScript doesn't seem to work that way. What I would like to do is see a message that displays the text file name and the offending record. How can I do this in VBScript?
 
You use the second style of VB error handling, which tends to be more structured:
Code:
On Error Resume Next
[COLOR=red]-statement to be error-trapped-[/color]
If Err.Number <> 0 Then
  On Error GoTo 0
  [COLOR=red]-process good result-[/color]
Else
  CacheErrNum = Err.Number
  On Error GoTo 0
  Err.Clear  
  [COLOR=red]-handle error case-[/color]
End If
 
Here's how I translated that

On Error Resume Next
objRecordSetSQL.Update*************THIS IS THE LINE THAT BLOWS UP
If Err.Number <> 0 Then
On Error GoTo 0
Else
MsgBox BirthDate & "-" & DateAssigned & "-" & DateModified & chr(10) & chr(13) & FlName & "-" & RecStr
On Error GoTo 0
Err.Clear
End If

It gives me a message box for each record processed. I would like to see it only for the record that is having the problem. Obviously, I'm doing something wrong.
 
I think that he had a '<>' where he needed an '='. Usually if the Err.Number = 0 then everything is OK. If it is something other than 0 then some kind of error happened. So:

Code:
On Error Resume Next
-statement to be error-trapped-
If Err.Number = 0 Then
  On Error GoTo 0
  -process good result-
Else
  CacheErrNum = Err.Number
  On Error GoTo 0
  Err.Clear  
  -handle error case-
End If

But I'm not familiar with the "On Error GoTo 0" syntax in VBScript.
 
Thanks. That seems to have done the trick
 
Yep, exactly. I reversed the two paths and forgot to reverse the sense of the test!

Sorry for misdirecting you with a boneheaded error.
 
You might want to turn off error trapping and clear the error before doing anything else though... just so that any additional unplanned error gets brought to your attention.
 
why not just simplify it do the action when there's an error, rather than "do nothing when there's no error?:
Code:
On Error Resume Next
If Err.Number > 0 Then
   MsgBox BirthDate & "-" & DateAssigned & "-" & DateModified & chr(10) & chr(13) & FlName & "-" & RecStr
   Err.Clear
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top