473,399 Members | 3,832 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,399 software developers and data experts.

Error Handling For A Recordset

Tom
What is the code for a recordset in error handling code when the error may
or may not occur before the recordset was created? I tried the code below
but get the message:
Compile Error
Invalid Use Of Object
and Nothing is highlighted when I click debug.

If RstSurveySection <> Nothing Then
RstSurveySection.Close
Set RstSurveySection = Nothing
End If

Thanks!

Tom
Nov 13 '05 #1
6 5031

"Tom" <no***@email.com> wrote in message
news:_J******************@newsread3.news.atl.earth link.net...
What is the code for a recordset in error handling code when the error may
or may not occur before the recordset was created? I tried the code below
but get the message:
Compile Error
Invalid Use Of Object
and Nothing is highlighted when I click debug.

If RstSurveySection <> Nothing Then
RstSurveySection.Close
Set RstSurveySection = Nothing
End If

Thanks!

Tom


If Not RstSurveySection Is Nothing Then
....
Nov 13 '05 #2
Should it be NULL instead of Nothing? just a guess here

Nov 13 '05 #3
Tom
"If Not RstSurveySection Is Nothing Then"
worked fine!

Tom
"Tom" <no***@email.com> wrote in message
news:_J******************@newsread3.news.atl.earth link.net...
What is the code for a recordset in error handling code when the error may
or may not occur before the recordset was created? I tried the code below
but get the message:
Compile Error
Invalid Use Of Object
and Nothing is highlighted when I click debug.

If RstSurveySection <> Nothing Then
RstSurveySection.Close
Set RstSurveySection = Nothing
End If

Thanks!

Tom

Nov 13 '05 #4
Tom wrote:
"If Not RstSurveySection Is Nothing Then"
worked fine!


You still need to error trap, consider this

Set RstSurveySection = db.openrecordset....
....some code
RstSurveySection.close
.... some code
if not RstSurveySection is nothing then
RstSurveySection.close ' <<<< Error will occur here
set RstSurveySection=nothing
end if

RstSurveySection can be closed or become invalid even if you don't
explicitly close it yourself, VBA isn't perfect or invunerable to cosmic
rays :-)

--
[OO=00=OO]
Nov 13 '05 #5
"Trevor Best" <no****@besty.org.uk> wrote in message
news:42**********************@news.zen.co.uk...
Tom wrote:
"If Not RstSurveySection Is Nothing Then"
worked fine!


You still need to error trap, consider this

Set RstSurveySection = db.openrecordset....
...some code
RstSurveySection.close
... some code
if not RstSurveySection is nothing then
RstSurveySection.close ' <<<< Error will occur here
set RstSurveySection=nothing
end if

RstSurveySection can be closed or become invalid even if you don't
explicitly close it yourself, VBA isn't perfect or invunerable to cosmic
rays :-)

--
[OO=00=OO]

Indeed, so you might have it as part of your 'exit block' like:

Exit_MySub:

On Error Resume Next

If Not RstSurveySection Is Nothing Then
RstSurveySection.Close
Set RstSurveySection = Nothing
End If

Exit Sub
However, if you have the above error handling, it is doubtful whether it is
even worth checking if the object is nothing or not. In other words, you
might as well have:

Exit_MySub:
On Error Resume Next
RstSurveySection.Close
Set RstSurveySection = Nothing
Exit Sub
Having said that, I tend to write code as in the first example - it just
means that my coding always looks recognisable.


Nov 13 '05 #6
Justin Hoffman wrote:
Indeed, so you might have it as part of your 'exit block' like:

Exit_MySub:

On Error Resume Next

If Not RstSurveySection Is Nothing Then
RstSurveySection.Close
Set RstSurveySection = Nothing
End If

Exit Sub
However, if you have the above error handling, it is doubtful whether it is
even worth checking if the object is nothing or not. In other words, you
might as well have:

Exit_MySub:
On Error Resume Next
RstSurveySection.Close
Set RstSurveySection = Nothing
Exit Sub
Having said that, I tend to write code as in the first example - it just
means that my coding always looks recognisable.


I do like the second, I see no point in checking and preventing a
possible error if you're going to ignore it anyway.

--
[OO=00=OO]
Nov 13 '05 #7

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

Similar topics

8
by: Niraj Khandwala | last post by:
Dear all, this error handling has been a pain since so manya days using ASP 3.0 with IIS 5.0 on W2K server Created a custom error page using the Server.GetLastError() and works fine in most of...
8
by: Pete | last post by:
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...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
3
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
10
by: David | last post by:
Hello I am trying to collect errors and record them in a table instead of a popup message stopping my code. It seems to work ok, but when I try to add ERR.Description to my code it fails on Syntax...
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.