isobject is not working the way i think it should
isobject is not working the way i think it should
am 11.05.2006 21:33:42 von mgreenway
Sub RecordsetClose()
if isObject(RS) = True then
if RS.State = 1 then <<<< ERROR LINE
RS.Close
end if
end if
if IsObject(conn) = True then
if Conn.State = 1 then
Conn.Close
end if
end if
set RS = Nothing
Set Conn = Nothing
End Sub
Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'RS'
Re: isobject is not working the way i think it should
am 11.05.2006 22:50:12 von Steven Burn
You can't use it as an object if you've not *delcared* it as such.
--
Regards
Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk
Keeping it FREE!
wrote in message
news:1147376022.198115.226560@g10g2000cwb.googlegroups.com.. .
> Sub RecordsetClose()
> if isObject(RS) = True then
> if RS.State = 1 then <<<< ERROR LINE
> RS.Close
> end if
> end if
> if IsObject(conn) = True then
> if Conn.State = 1 then
> Conn.Close
> end if
> end if
> set RS = Nothing
> Set Conn = Nothing
> End Sub
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A8)
> Object required: 'RS'
>
Re: isobject is not working the way i think it should
am 11.05.2006 22:55:09 von mgreenway
I figured that the IsObject() = true before I try to use something as
an object would cover me on that one...
Re: isobject is not working the way i think it should
am 11.05.2006 23:18:08 von unknown
1. I recommend you not abstract something as basic as closing a recordset.
Let that just be in the flow of your code so that the next programmer hired
off the street can follow the logic of your code without having to jump
around all over the place looking for subroutines written to replace two or
three lines of code.
2. You could always encapsulate it in an "on error resume next/on error
goto 0"
Ray at work
wrote in message
news:1147376022.198115.226560@g10g2000cwb.googlegroups.com.. .
> Sub RecordsetClose()
> if isObject(RS) = True then
> if RS.State = 1 then <<<< ERROR LINE
> RS.Close
> end if
> end if
> if IsObject(conn) = True then
> if Conn.State = 1 then
> Conn.Close
> end if
> end if
> set RS = Nothing
> Set Conn = Nothing
> End Sub
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A8)
> Object required: 'RS'
>
Re: isobject is not working the way i think it should
am 12.05.2006 00:36:54 von Steven Burn
Nope, it won't .......... in VBS, ALL var's are variants, not objects, longs
etc etc etc etc
Thus, what you need to do is tell your sub what it is processing.... thus;
Option Explicit
Dim lState, oRS
'// Some DB conn code
'// Some RS loop or whatever
lState = oRS.State
Call MySub()
'// End RS loop
Sub MySub(oRS)
If IsObject(oRS) Then
'// At this point, whilst it has verified oRS is an object, it ain't
got a clue
'// so we need to either re-delcare it, or take a pre-def public var
Select Case lState
Case 1: '// Do something
Case Else: '// Do something else
End Select
End If
End Sub
--
Regards
Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk
Keeping it FREE!
wrote in message
news:1147380909.696834.99720@j73g2000cwa.googlegroups.com...
> I figured that the IsObject() = true before I try to use something as
> an object would cover me on that one...
>
Re: isobject is not working the way i think it should
am 12.05.2006 02:41:12 von Bob Lehmann
RS and conn are probably out of scope inside the Sub, depending on how you
have declared those variables.
Either make them arguments to the Sub or properly declare them outside of
the Sub.
Bob Lehmann
wrote in message
news:1147376022.198115.226560@g10g2000cwb.googlegroups.com.. .
> Sub RecordsetClose()
> if isObject(RS) = True then
> if RS.State = 1 then <<<< ERROR LINE
> RS.Close
> end if
> end if
> if IsObject(conn) = True then
> if Conn.State = 1 then
> Conn.Close
> end if
> end if
> set RS = Nothing
> Set Conn = Nothing
> End Sub
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A8)
> Object required: 'RS'
>
Re: isobject is not working the way i think it should
am 12.05.2006 09:40:14 von Anthony Jones
wrote in message
news:1147376022.198115.226560@g10g2000cwb.googlegroups.com.. .
> Sub RecordsetClose()
> if isObject(RS) = True then
> if RS.State = 1 then <<<< ERROR LINE
> RS.Close
> end if
> end if
> if IsObject(conn) = True then
> if Conn.State = 1 then
> Conn.Close
> end if
> end if
> set RS = Nothing
> Set Conn = Nothing
> End Sub
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A8)
> Object required: 'RS'
>
The fault is that whilst the RS variabe does exist it contains Nothing.
Run this from a VBS file:-
MsgBox IsObject(Nothing) ' Returns True
Nothing is a null pointer to an object.
Putting a null pointer to an object in a variable of variant type causes the
variant to have the VT type indicating it is storing an object pointer.
All IsObject does is looking at the VT type of the variant and return true
if it's an object type, it does care that the actual pointer being stored is
null. Hence it returns true even though the variable is nothing.
Three things you should do:-
If you haven't already got Option Explicit put it in.
Where declaring the RS variable use this:-
Dim RS : Set RS = Nothing
this ensures that RS is always of the object type even if it doesn't get
used.
Test for an existance of an object in RS with:-
If Not RS Is Nothing Then
...
End If
Having said all that, I agree with Ray this is an abstraction too far, just
close the recordset and connection in line with it's usage.
Anthony.