?
Solved

In what occassion we need to apply on error goto 0 ?

Posted on 2003-02-23
5
Medium Priority
?
228 Views
Last Modified: 2010-05-01
I know on error goto 0 is To DISABLE the error handler, now the other question is WHEN we must use on error goto O, Pls give one simple and easy example (coz difficult example sometimes won't make the things clear), thank you
0
Comment
Question by:bvlgary
[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
5 Comments
 
LVL 2

Expert Comment

by:JSMCM
ID: 8006819
On Error Goto doesn't disable the handler, it only clears the last error.

Lets say you want to perform a calculation based on users input. If the user enters a letter, you will get a runtime error, so you put in some error handling to avoid the program crashing:

Private Sub Command1_Click()
 On Error Resume Next  
 Text3 =  Text1 + Text2
 On Error goto 0
End Sub

now, if your user enters a letter, your program won't crash, but it will trap( record) an error. Using the on error goto 0 clears the error so that you can trap other errors.
0
 
LVL 2

Expert Comment

by:JSMCM
ID: 8006848
You use it after you have handled an error
0
 

Author Comment

by:bvlgary
ID: 8006920
what happen if i ignore the on error goto 0, so the code become :

Private Sub Command1_Click()
On Error Resume Next  
Text3 =  Text1 + Text2
End Sub

Does it make any difference?
0
 
LVL 2

Accepted Solution

by:
JSMCM earned 200 total points
ID: 8007147
any errors after the on error resume next statement will be ignored.

Lets assume you have the following

Private Sub Command1_Click()
Dim T1 as Integer
Dim T2 as Integer
Dim T3 as Integer

On Error resume next
T1 = Text1.Text
T2 = Text2.Text

T3 = T2 + T1

Text3.Text = T3


Open "DDFDF" for input as #1

End Sub


If the file DDFDF doesn't exist, you want to know about it sothat you can create it, or warn a user it doesn't exist, or the path is wrong or something, but at the end of the day, you want to know about it.

Running the program like above will not let you know about it, because it see's the On Error resume next, resumes the next line of code after seeing the error with putting a letter into an integer.

Then it comes across the error of not finding a file, and again just resumes the next line. if the next line is another error, it will go to the line after that.

if you wrote the code like this:

Private Sub Command1_Click()
Dim T1 as Integer
Dim T2 as Integer
Dim T3 as Integer

On Error resume next
T1 = Text1.Text
T2 = Text2.Text

T3 = T2 + T1

Text3.Text = T3
 on error goto 0

 on error goto NoFile
Open "DDFDF" for input as #1

exit sub
NoFile:
msgbox "No File Found"
resume next

End Sub

It would see the Letter into an integer error, resume next line. Then it would come across the on error goto 0 statement, and clear the error handler.

Now, when it comes across the second error, it can handle it correclty.


Also, when you are putting error handling into your code, you might want to check if an error occured before running some code. so you might say:

If Err.Number <> 0 then
  Do something because there was an error

Else
 Do something else because there was no error.
End If

If you never cleared the error number from a previous, unrelated error, you would in fact now be executing the wrong code.

Staments like this are quite common, so clearing errors is important.
0
 

Author Comment

by:bvlgary
ID: 8007238
Perfect!!! This is what i need, thank you
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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

752 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