By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,448 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,448 IT Pros & Developers. It's quick & easy.

Error Handling For A Recordset

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a
Should it be NULL instead of Nothing? just a guess here

Nov 13 '05 #3

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.