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!

Abt Error Trapping

Status
Not open for further replies.

Haheehi

Programmer
Sep 4, 2000
28
SG
can any1 explain:
on error goto next

where should i place this line? [sig][/sig]
 
You may be getting mixed up with 2 error handling methods:

1. Use On Error Resume Next whenever you want to ignore any errors that occur and the program will continue on the line after the one that produced the error.

For example, when compacting a database called data.mdb, I normally rename the database to backup.mdb and compact using the original name. To do this, I would delete any existing backup.mdb using:
' delete existing backup.mdb if it exists
On Error Resume Next
Kill App_Path & "backup.mdb"

I use the On Error Resume Next statement to tell the program to continue if there was an error if say there was not a backup.mdb to delete.

2. Use On Error GoTo EHandler (the name of the code piece to handle the errors is up to you, so long as you have the tag defined in your code that you use on your On Error .. line) to trap errors and go to a specific piece of code.

For example, after the two lines of code above, I would then have:

On Error GoTo Err_Compact_Database

to go to this code if an error occurred while doing the compacting.

The tag Err_Compact_Database would then be placed at the end of the sub, as in:

Private Sub Compact_Database()

' delete existing backup.mdb if it exists
On Error Resume Next
Kill App_Path & "backup.mdb"
On Error GoTo Err_Compact_Database
'other code to do the compacting

Exit Sub ' this is needed so that the error handling code _
is not executed every time

Err_Compact_Database:
MsgBox Err.Number & " ; " & Err.Description, vbOKOnly + vbExclamation, gsAPPTITLE

End Sub

Another useful way of this type of error handling is to give the user the option of retry, ignore or cancel on errors.
After the message box,
1. if the user clicks retry do Resume;
2. if the user clicks ignore do Resume Next (to continue after the line that gave the error); and
3. if the user clicks cancel do Exit Sub [sig]<p>Simon<br>[/sig]
 
That means that On error Resume Next is to skip the error occur on the next line, not the line before.

But we can also put On error Resume Next everywhere as we wouldn't want any errors at all. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top