Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamically create recordset objects

1 view
Skip to first unread message

Brian D

unread,
Nov 20, 2006, 2:45:23 PM11/20/06
to
I am trying to create a sub or function that will allow me to pass a
name to it and then it will create an adodb.recordset. I know my code
is wrong but this is what I am trying to accomplish:

sub createRS(tmpRS)
eval("set " & tmpRS & " = server.createobject('adodb.recordset')")
end sub

Is this even possible?

Thanks
Brian

Bob Barrows [MVP]

unread,
Nov 20, 2006, 3:17:05 PM11/20/06
to
Brian D wrote:
> I am trying to create a sub or function that will allow me to pass a
> name to it and then it will create an adodb.recordset. I know my
> code is wrong but this is what I am trying to accomplish:
>
> sub createRS(tmpRS)
> eval("set " & tmpRS & " = server.createobject('adodb.recordset')")

Ughhh! Eval/Execute is evil!!
http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx

You're not planning on storing these objects in Application or Session are
you?
http://www.aspfaq.com/2053

> end sub
>
> Is this even possible?
>

You best course depends on what your purpose is, but for most cases, just
use a dictionary object to maintain a collection of named objects.

dim dictRS
function getNamedRS(tmpRS)
dim rs
if not isobject(dictRS) then
set dictRS=CreateObject("Scripting.Dictionary")
set rs = createobject(adodb.recordset")
dictRS.Add tmpRS, rs
else
if dictRS.exists(tmpRS) then
set rs=dictRS.Item(tmpRS)
else
set rs = createobject(adodb.recordset")
dictRS.Add tmpRS, rs
end if
end if
set getNamedRS = rs
end function

However, don't think about storing any of these objects in Application or
Session.

--
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"


Daniel Crichton

unread,
Nov 21, 2006, 6:00:29 AM11/21/06
to

Why not just pass in your named object reference, and use that? By default
vars passed into a sub or function are ByRef, so it should work fine.

eg.

sub createRS(tmpRS)
set tmpRS = server.createobject("adodb.recordset")
end sub

dim oMyRS1 as object, oMyRS2 as object

createRS oMyRS1
createRS oMyRS2


you should have two recordset objects, one called oMyRS1, the other oMyRS2

or use a function instead:

function createRS()
set createRS = server.createobject("adodb.recordset")
end function

set oMyRS1 = createRS()
set oMyRS2 = createRS()


Bob's dictionary suggestion is handy for maintaining a dynamic number of
objects where you don't know, and so can't represent easily in code, the
number of objects you'll be using.

Dan


0 new messages