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

Error trapping not working 2

Status
Not open for further replies.

owentmoore

Technical User
Jul 20, 2006
60
IE
Hi All,

I am having a problem with error trapping a routine I have setup. If I enter an incorrect file path in the second file location listed in the code below, the error statement does not seem to work correctly. It should jump to line "End_Now:" but instead throws up error Run Time Error 76, Path Not Found.

The reason I need this to work is that all the users using this file may not have access to the file path, and if they don't I want the file to continue opening without alerting them.

Code is as follows:

Code:
Private Sub Workbook_Open()

Dim x As Integer
On Error GoTo log_it
Open "\\DPYIECOFPS01\HomeH$\omoore\Process Support\Consumables\log_me.txt" For Input As #1
Input #1, x
log_it:
x = x + 1
Close
On Error GoTo End_Now

Open "\\DPYIECOFPS01\HomeH$\omoore\Process Support\Consumables\log_me.txt" For Append As #1
Print #1, Environ$("username") + " Open " + " - " + Date$ + " - " + Time$
Close
GoTo End_Now
End_Now:

End Sub

Thanks
Owen
 
You need to do something within the EndNow: portion in order not to get the error message.

So, the best thing would do would be to add a conditional statement within that portion of code. Something like this:

Code:
Private Sub Workbook_Open()

Dim x As Integer
On Error GoTo log_it
Open "\\DPYIECOFPS01\HomeH$\omoore\Process Support\Consumables\log_me.txt" For Input As #1
Input #1, x
log_it:
x = x + 1
Close
On Error GoTo End_Now

Open "\\DPYIECOFPS01\HomeH$\omoore\Process Support\Consumables\log_me.txt" For Append As #1
Print #1, Environ$("username") + " Open " + " - " + Date$ + " - " + Time$
Close
GoTo End_Now
End_Now:
[highlight]   If Err.Number = 76 Then
      Exit Sub
   Else
      MsgBox "An error has occurred:" & Chr(13) & Chr*(13) & _
             "Error Number" & Chr(9) & "Error Description" & _
             Err.Number & Chr(9) & Chr(9) & Err.Description
[/highlight]
End Sub
 

The difficulty comes from not understanding how error handling code works. Which in turn comes from the fact that error handling code looks like any other code but is treated differently.

While handling an error, other errors are ignored.

An error handler should end with a Resume statement.

In your case, if you replace the line of code
[tt]
On Error GoTo End_Now
[/tt]
with
[tt]
Resume End_Now
[/tt]
you should get closer to the result you are looking for.

Better still is to move the error handling code to the bottom of the routine (following an "Exit Sub" statement) which would make the error handling code stand out for what it is.

BTW the statement "On Error GoTo End_Now" merely resets the error handling code entry for the next error, but has no effect on the current handler (as you found out).





 
Thanks Guys!

Some interesting stuff, especially about the way errors are handled...good to nkow. I've got the code to work using the Resume statement.

Owen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top