473,396 Members | 1,996 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,396 software developers and data experts.

Error-trapping question

I'm trying to improve my code so that when I open a recordset object,
I can absolutely guarantee it is closed and is set = Nothing. I have
read some old threads and they all say to use the functional
equivalent of the following code:

Public Sub CloseRecordset()
On Error GoTo Sub_Error
Dim rs As Recordset

rs.FindFirst "This Will Error"

Sub_Exit:
On Error Resume Next
rs.Close '<----"Object variable or with block not set"
Set rs = Nothing

Sub_Error:
GoTo Sub_Exit
End Sub
However, *THIS CODE DOES NOT WORK*. It should attempt to "rs.Close"
and fail, and move to the next record as dictated by the "On Error
Resume Next" line. But it doesn't; it pops up the Debug/End/Help box
here (the default error handling).

I'm using Access 97, and yes, this is cut/pasted directly out of my
test function. I understand that originally, I have it go to
Sub_Error whenever an error occurs. When I get to the exit point (the
Sub_Exit label), it should switch to the "resume next" behavior, but
instead apparently resets the error trapping to default. What am I
doing wrong?
Pete
Nov 12 '05 #1
8 3350
Pete, you need to add the line:
Exit Sub
immediately after:
Set rs = Nothing.

If you do not exit sub at this point, the code falls into the error trapping
routine, and is sent back to the Sub_Exit label.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Pete" <ps********@zombieworld.com> wrote in message
news:98**************************@posting.google.c om...
I'm trying to improve my code so that when I open a recordset object,
I can absolutely guarantee it is closed and is set = Nothing. I have
read some old threads and they all say to use the functional
equivalent of the following code:

Public Sub CloseRecordset()
On Error GoTo Sub_Error
Dim rs As Recordset

rs.FindFirst "This Will Error"

Sub_Exit:
On Error Resume Next
rs.Close '<----"Object variable or with block not set"
Set rs = Nothing

Sub_Error:
GoTo Sub_Exit
End Sub
However, *THIS CODE DOES NOT WORK*. It should attempt to "rs.Close"
and fail, and move to the next record as dictated by the "On Error
Resume Next" line. But it doesn't; it pops up the Debug/End/Help box
here (the default error handling).

I'm using Access 97, and yes, this is cut/pasted directly out of my
test function. I understand that originally, I have it go to
Sub_Error whenever an error occurs. When I get to the exit point (the
Sub_Exit label), it should switch to the "resume next" behavior, but
instead apparently resets the error trapping to default. What am I
doing wrong?
Pete

Nov 12 '05 #2
step thru and tell us which line is crashing
Nov 12 '05 #3
Try:
Dim rs As DAO.Recordset

If that line generates an error, it is a references issue. Details:
http://allenbrowne.com/ser-38.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"phappyman" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

Thanks, but that isn't the problem. I built the test sub just to make
absolutely sure that what I was writing happened. The reason why I
forgot the "Exit Sub" line is because my subroutine *never got that
far*. Here is the (updated) subroutine:

Public Sub CloseRecordset()

On Error GoTo Sub_Error

Dim rs As Recordset

rs.FindFirst "This Will Error"

Sub_Exit:

On Error Resume Next

rs.Close

Set rs = Nothing

Exit Sub

Sub_Error:

GoTo Sub_Exit

End Sub



The problem is the same as before, but the obvious (unrelated) error has
been fixed.

Pete

Original question:
> I'm trying to improve my code so that when I open a recordset object,

> I can absolutely guarantee it is closed and is set = Nothing. I have

> read some old threads and they all say to use the functional

> equivalent of the following code:

> Public Sub CloseRecordset()

> On Error GoTo Sub_Error

> Dim rs As Recordset

> rs.FindFirst "This Will Error"

> Sub_Exit:

> On Error Resume Next

> rs.Close '<----"Object variable or with block not set"

> Set rs = Nothing

> Sub_Error:

> GoTo Sub_Exit

> End Sub

> However, *THIS CODE DOES NOT WORK*. It should attempt to "rs.Close"

> and fail, and move to the next record as dictated by the "On Error

> Resume Next" line. But it doesn't; it pops up the Debug/End/Help box

> here (the default error handling).

> I'm using Access 97, and yes, this is cut/pasted directly out of my

> test function. I understand that originally, I have it go to

> Sub_Error whenever an error occurs. When I get to the exit
> point (the

> Sub_Exit label), it should switch to the "resume next" behavior, but

> instead apparently resets the error trapping to default. What am I

> doing wrong?

> Pete

--
Posted via http://dbforums.com

Nov 12 '05 #4
Let me start over. This is not a references issue-the code runs. I
am using Access 97 and there isn't an ADO reference set, so the
ADODB/DAO precedence is not an issue.
I'm trying to improve my code so that when I open a recordset object,
I can absolutely guarantee it is closed and is set = Nothing. I have
read some old threads and they all say to use the functional
equivalent of the following code, which uses the "On Error Resume
Next" at the function's exit point:

'The numbers "01"-"12" at the beginning of the lines
'were added for readability
'Please note that my function does not have those
'numbers in the code, so no kneejerk replies please

01 Public Sub CloseRecordset()
02 On Error GoTo Sub_Error
03 Dim rs As Recordset

04 rs.FindFirst "This Will Error"

05 Sub_Exit:
06 On Error Resume Next
07 rs.Close '<----"Object variable or with block not set"
'Above error message appears on previous
'line; it shouldn't

08 Set rs = Nothing

09 Exit Sub

10 Sub_Error:
11 GoTo Sub_Exit
12 End Sub
This is the question: why does the On Error Resume Next not work? I
have stepped through the code, and it goes 01-02-03-04-10-11-05-06-07
and then POPS UP the default Access error handling on line 07.
Remember that line 06 has just instructed the environment to "Resume
Next" on an error. WHY DOES "On Error Resume Next" FAIL TO WORK AS
ADVERTISED?

That is my question.
Pete
Nov 12 '05 #5
Between steps 03 and 04 you willneed to OPEN the recordset. You can't find
a record until the set is open
Nov 12 '05 #6
"hal boyles" <ha********@timeinc.com> wrote in message news:<bk**********@inntp-m1.news.aol.com>...
Between steps 03 and 04 you willneed to OPEN the recordset. You can't find
a record until the set is open


Thanks, but no thanks. The point is not that the function WORKS.
Because the function doesn't DO ANYTHING ANYWAY. So stop critiquing
the FAKE FUNCTION I WROTE TO PROVE A POINT.

I'm tired of people not reading my post. If I'm being unclear, sorry.
My problem was that the line "On Error Resume Next" does not work as
advertised. Read my other most recent post again.

Yes, I'm probably being too rude, but so far I'm 0 for 4 on replies.
Nov 12 '05 #7
"Pete" <ps********@zombieworld.com> wrote in message
news:98*************************@posting.google.co m...
"hal boyles" <ha********@timeinc.com> wrote in message

news:<bk**********@inntp-m1.news.aol.com>...
Between steps 03 and 04 you willneed to OPEN the recordset. You can't find
a record until the set is open


Thanks, but no thanks. The point is not that the function WORKS.
Because the function doesn't DO ANYTHING ANYWAY. So stop critiquing
the FAKE FUNCTION I WROTE TO PROVE A POINT.

I'm tired of people not reading my post. If I'm being unclear, sorry.
My problem was that the line "On Error Resume Next" does not work as
advertised. Read my other most recent post again.

Yes, I'm probably being too rude, but so far I'm 0 for 4 on replies.


The only thing I notice that is different from how I usually do things is that I
would use...

10 Sub_Error:
11 Resume Sub_Exit
12 End Sub

....instead of GoTo Sub_Exit. I do exactly what your code example is attempting and I
do not get an error on the rs.Close line.
Nov 12 '05 #8
> The only thing I notice that is different from how I usually do things is that I
would use...

10 Sub_Error:
11 Resume Sub_Exit
12 End Sub

...instead of GoTo Sub_Exit. I do exactly what your code example is attempting and I
do not get an error on the rs.Close line.


Thanks! This was exactly my problem. I changed the "GoTo" to Resume
and it worked flawlessly.
Pete
Nov 12 '05 #9

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...
2
by: Gregory | last post by:
Hi, One of the disadvantages of using error handling with error codes instead of exception handling is that error codes retuned from a function can be forgotten to check thus leading to...
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....
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
1
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
0
by: mchuc7719 | last post by:
Hello, I have a Vb.Net 2005 ClassLibrary, when I try to compile using MSBee, only get errors. Before I to run the command line, I open in notepad the .vbproj and I was add the next line: ...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
4
by: jk2l | last post by:
Error 10 error LNK2019: unresolved external symbol __imp__glBindTexture@8 referenced in function "public: void __thiscall GLTexture::Use(void)" (?Use@GLTexture@@QAEXXZ) GLTexture.obj Error 11 error...
1
by: Deepath G | last post by:
This is deepath.. I am getting some linker error when i am trying to connect Websphere MQ using Borland C++ Builder 2006 using imqi.hpp on windows. Error Message ----------------------- ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.