Can I not create an object in one sub and access it from another?


sub housekeeping
set rs=server.createobject("adodb.recordset")
end sub

sub printStatistics
rs.open "select max(date) from syslog.mxdb",conn
end sub


Microsoft VBScript runtime error '800a01a8'

Object required: 'rs'

/mx/new.asp, line 68

Re: object in a sub [object missing?] by Joe

Joe
Sun Nov 06 03:09:42 CST 2005

"Dom" <dom@invalid> wrote in message
news:%23ScpmEq4FHA.2888@tk2msftngp13.phx.gbl...
> Can I not create an object in one sub and access it from another?
>
>
> sub housekeeping
> set rs=server.createobject("adodb.recordset")
> end sub
>
> sub printStatistics
> rs.open "select max(date) from syslog.mxdb",conn
> end sub
>
>
> Microsoft VBScript runtime error '800a01a8'
>
> Object required: 'rs'
>
> /mx/new.asp, line 68
If you Dim rs outside of the sub then it will be available to all
procedures.
This is generally poor programming practice as it means other code can also
modify rs by accident but is acceptable for quick and dirty scripts that
others will not need to modify.

Joe



Re: object in a sub [object missing?] by Dom

Dom
Sun Nov 06 04:47:55 CST 2005

I dim'd rs outside of all procedures and turned up with a different
error. I am attempting to modularize my program by moving everything
into functions/subs.

The only portion of code not in a function/sub:

housekeeping
select case qtype
case 0
printStatistics
case 1
printLog
end select
wrap




ADODB.Recordset error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in
conflict with one another.

/mx/new.asp, line 68




ORIGINAL:

sub housekeeping
set rs=server.createobject("adodb.recordset")
end sub

sub printStatistics
rs.open "select max(date) from syslog.mxdb",conn
end sub


Microsoft VBScript runtime error '800a01a8'

Object required: 'rs'

/mx/new.asp, line 68

Re: object in a sub [object missing?] by Joe

Joe
Sun Nov 06 05:36:17 CST 2005

See inline:

"Dom" <dom@invalid> wrote in message
news:uJ5km%23r4FHA.2352@TK2MSFTNGP12.phx.gbl...
>I dim'd rs outside of all procedures and turned up with a different error.
>I am attempting to modularize my program by moving everything into
>functions/subs.
>
> The only portion of code not in a function/sub:
>
> housekeeping
> select case qtype
> case 0
> printStatistics
> case 1
> printLog
> end select
> wrap
>
If this is the only thing not in a sub where is your Dim rs statement?

Joe

>
>
>
> ADODB.Recordset error '800a0bb9'
>
> Arguments are of the wrong type, are out of acceptable range, or are in
> conflict with one another.
>
> /mx/new.asp, line 68
>
>
>
>
> ORIGINAL:
>
> sub housekeeping
> set rs=server.createobject("adodb.recordset")
> end sub
>
> sub printStatistics
> rs.open "select max(date) from syslog.mxdb",conn
> end sub
>
>
> Microsoft VBScript runtime error '800a01a8'
>
> Object required: 'rs'
>
> /mx/new.asp, line 68



Re: object in a sub [object missing?] by Dom

Dom
Sun Nov 06 06:46:56 CST 2005

I tried it with the dim rs statement and it didn't work, so I removed
it. I had added it right after the functions and subs (I also tried it
at the top [same result]), like so:

[functions/subs]

dim rs
housekeeping
select case qtype
case 0
printStatistics
case 1
printLog
end select
wrap

Re: object in a sub [object missing?] by Bob

Bob
Sun Nov 06 07:37:46 CST 2005

Dom wrote:
> I tried it with the dim rs statement and it didn't work, so I removed
> it. I had added it right after the functions and subs (I also tried it
> at the top [same result]), like so:
>
> [functions/subs]
>
> dim rs
> housekeeping
> select case qtype
> case 0
> printStatistics
> case 1
> printLog
> end select
> wrap

This should work. Here is a repro where it works for me:
<%
dim rs
sub InitializeRS
set rs=CreateObject("adodb.recordset")
end sub
sub CreateRS
with rs.Fields
.append "col1",adInteger
.append "col2",adVarChar,20
end with
with rs
.Open
.AddNew array(0,1), array(1,"One")
.AddNew array(0,1), array(2,"Two")
.MoveFirst
end with
end sub
Sub DisplayRS
Response.Write rs.GetString(adClipString,," | ","<BR>")
end sub
InitializeRS
CreateRS
DisplayRS
rs.Close
%>

Joe mentioned a potential drawback with using global variables. A
recommended technique is to use only local variables, using arguments to
pass them between methods. Like this:
<%
function InitializeRS
dim rs
set rs=CreateObject("adodb.recordset")
set InitializeRS=rs
end function
sub CreateRS(byref pRS)
with pRS.Fields
.append "col1",adInteger
.append "col2",adVarChar,20
end with
with pRS
.Open
.AddNew array(0,1), array(1,"One")
.AddNew array(0,1), array(2,"Two")
.MoveFirst
end with
end sub
Sub DisplayRS
dim rs
set rs=InitializeRS
CreateRS rs
Response.Write rs.GetString(adClipString,," | ","<BR>")
end sub
DisplayRS
%>

HTH,
Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"



Re: object in a sub [object missing?] by Dom

Dom
Sun Nov 06 18:19:18 CST 2005

Thanks for the input. I just ended up removing the housekeeping and wrap
routines from modules and everything is working well. I am satisfied
with the level of modularization I have achieved.