473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

I'm goofed up in On Error

I thought that I had a grasp of how VBA error handling functioned, but have
just become painfully aware that I don't.

I thought that the "On Error GoTo 0" in the second sub below would turn off
error handling for that procedure. I single stepped the program, it gets an
error on the last line (the Name Workdir... line), the next command to
execute is Resume SkipReport. I know why the error occurs, but I don't
understand how it resumes in the error handler from the other procedure.

Would someone kindly explain this for me.

-----------------------------------------------------
Public Sub BatchPrint()

On Error GoTo BadReport
ChangeName "File1", "File2"
[other code in here removed]
Exit Sub

BadReport:

'====== Continues here
Resume SkipReport
End Sub
-------------------------------------------------------------
Private Sub ChangeName(FromName As String, ToName As String)

On Error Resume Next
Kill ToName

On Error GoTo 0
' ===== Errors here
Name WorkDir & FromName As WorkDir & ToName

End Sub
-------------------------------------------------------
Nov 12 '05 #1
2 2883
"Randy Harris" <ra***@SpamFree.com> wrote in message
news:Ro**********************@newssvr28.news.prodi gy.com...
I thought that I had a grasp of how VBA error handling functioned, but have just become painfully aware that I don't.

I thought that the "On Error GoTo 0" in the second sub below would turn off error handling for that procedure. I single stepped the program, it gets an error on the last line (the Name Workdir... line), the next command to
execute is Resume SkipReport. I know why the error occurs, but I don't
understand how it resumes in the error handler from the other procedure.

Would someone kindly explain this for me.

-----------------------------------------------------
Public Sub BatchPrint()

On Error GoTo BadReport
ChangeName "File1", "File2"
[other code in here removed]
Exit Sub

BadReport:

'====== Continues here
Resume SkipReport
End Sub
-------------------------------------------------------------
Private Sub ChangeName(FromName As String, ToName As String)

On Error Resume Next
Kill ToName

On Error GoTo 0
' ===== Errors here
Name WorkDir & FromName As WorkDir & ToName

End Sub
-------------------------------------------------------


When you say that "On Error GoTo 0" will "turn off error handling for that
procedure", that is correct in the sense of you no longer have any control
over what happens when an error occurs. Access will handle that error for
you - normally simply by displaying an error message. However, in this case
ChangeName has been called from BatchPrint so Access tries to do something
with that un-handled error by going back to BatchPrint and looking for
error-handling code there.

My advice would be to re-write ChangeName from a sub to a function which
returns either true or false but does not display an error message - you can
deal with the function returning false in your BatchPrint. You could write
more detailed error-handling, but here I have used On Error Resume Next to
cover both lines, so either true or false is returned. If this was my code
I might do a bit more checking of the file names and report the error more
specifically (illegal file name, lack of permissions, etc)

Private Function ChangeName(FromName As String, ToName As String) As Boolean
On Error Resume Next
Kill ToName
Name WorkDir & FromName As WorkDir & ToName
If Err.Number = 0 Then ChangeName = True
End Sub

Public Sub BatchPrint()

On Error GoTo BadReport

If ChangeName("File1", "File2") = True Then
Msgbox "Good"
Else
Msgbox "Bad"
End If

....
.....
etc
Fletcher


Nov 12 '05 #2
Think of error handling as a kind of stack.

As On Error statements (apart from On Error GoTo 0) are executed error
handlers are popped onto the stack (for the current procedure there can
onlty be one active error handler), an On Error GoTo 0 pops the error
handler for the procedure off the stack (so error handling for the current
procedure is indeed switched off).

When an error occurs the stack is then used (LIFO) in order to determine how
that error is to be handled.

So in the example you give (I've added line numbers to make it clearer)
' ******************************************
Public Sub BatchPrint()

10 On Error GoTo BadReport
20 ChangeName "File1", "File2"
30 [other code in here removed]
40 Exit Sub

BadReport:

'====== Continues here
50 Resume SkipReport
End Sub
-------------------------------------------------------------
Private Sub ChangeName(FromName As String, ToName As String)

10 On Error Resume Next
20 Kill ToName

30 On Error GoTo 0
' ===== Errors here
40 Name WorkDir & FromName As WorkDir & ToName

End Sub
' ******************************************

At Batchprint 10 an error handler is popped on so the stack could be
represented as
- BatchPrint10

Then another errohandler is popped on at ChangeName 10 so the stack could be
thought of as
- BatchPrint10
- ChangeName 10

Then On Error GoTo 0 at Line ChangeName30 pops the error handler off for
that procedure so the stack now looks like
- BatchPrint10

This then means that when the error occurs at ChangeName40 the current error
handler is at line 10 in BatchPrint. so the instruction at that line for
handling errors is used (i.e. jump to the BadReport label and execute the
staments after that).

Terry

"Randy Harris" <ra***@SpamFree.com> wrote in message
news:Ro**********************@newssvr28.news.prodi gy.com...
I thought that I had a grasp of how VBA error handling functioned, but have just become painfully aware that I don't.

I thought that the "On Error GoTo 0" in the second sub below would turn off error handling for that procedure. I single stepped the program, it gets an error on the last line (the Name Workdir... line), the next command to
execute is Resume SkipReport. I know why the error occurs, but I don't
understand how it resumes in the error handler from the other procedure.

Would someone kindly explain this for me.

-----------------------------------------------------
Public Sub BatchPrint()

On Error GoTo BadReport
ChangeName "File1", "File2"
[other code in here removed]
Exit Sub

BadReport:

'====== Continues here
Resume SkipReport
End Sub
-------------------------------------------------------------
Private Sub ChangeName(FromName As String, ToName As String)

On Error Resume Next
Kill ToName

On Error GoTo 0
' ===== Errors here
Name WorkDir & FromName As WorkDir & ToName

End Sub
-------------------------------------------------------

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
5
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information...
1
by: Aravind | last post by:
we have two files: 1. rc4.c (defines one function "create_pin()") 2. MyImpl.c(calling the function "create_pin()"),This implements JNI method. 1.When I am trying to create .dll file with one...
1
by: yanwan | last post by:
I met this problem in executing a c++ project in visual studio. Does anyone have suggestions to resolve "error lnk 2001"? --------------------Configuration: reconstruction - Win32...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
1
by: Dan Rolfe | last post by:
Ok I just kinda stumbled upon an error I made when I was building this database. It is a multitable relational DB consisting of 2 levels of grouping. Here is a rough design view: Owner...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.