views:

405

answers:

5

Sorry if the title is a bit vague. I couldn't think how else to say it!

I have a script which retrieves data into 3 different recordsets. They are called rs1, rs2 and rs3.

I have quite a large piece of code later on in the script so I have created a function to save save space etc.

Within the function I wish to use the information from the recordsets opened earlier. I have tried passing the name of the recordset to the function as follows:

Function displayData(recordsetName)

response.write(recordsetName.Source)

End Function

displayData("rs1")

However this is trying to show the results from a recordset called recordsetName and as that doesen't exist it is throwing up an error.

Somebody told me to use 'ByRef' however this throws up an error saying the the recordset does not exist.

How can I use a recorset name passed to a function as a parameter?

Thanks

+1  A: 

If what you want is that when you call it, it prints to the browser, what you should be using is a Sub in this way:

Sub displayData (rs)
    Response.Write (rs.Source)
End Sub    dim rs1 as new Recordset
'snip
displayData rs1 'note that calling subs in VB classic, doesn't use the enclosing ().

It has to be a Sub, as it won't be returning anything.

Otherwise, if displayData should return something, either the results of a calculation, or a response code, or the string written to the browser, you need to use a function

Function displayData (rs)
    Response.Write (rs.Source)
End Function

Dim rs1 as new Recordset
'snip
displayData (rs1)

Not shure why you are trying to do it in any other way.

voyager
The declaration should be Dim rst as this is asp classic.
jammus
Technically, one cannot use parenthesis when calling a Sub (although, in cases where only one parameter is passed, this is overlooked by the compiler as it seems to treat the parenthesis as part of the expression that is the first parameter, rather than the calling convention)
Jason Musgrove
You are right. I've corrected the answer. (never answer in a hurry)
voyager
A: 

You can't pass the name of the recordset you have to pass the recordset object.

Sub DisplayData(byref rst as recordset)
response.write(rst.Source)
End Sub
DisplayData(rs1)

I also changed your function to a Sub because in VB a function has to return something.

klabranche
It should be just Sub DisplayData(ByRef rst)
jammus
Also the ByRef is superflous.
AnthonyWJones
You are correct in that ByRef is the default but I like to be explicit in these cases. :)
klabranche
A: 

If you really wanted to pass in the name you could do

Sub DisplayData(rsName)
    Eval("Response.Write(" & rsName & ".Source)")
End Sub

DisplayData("rs1")

But don't. It's silly and can get you in to trouble. You should do it how the other guys say and pass in the recordset itself.

Sub DisplayData(rs)
    Response.Write rs.Source
End Sub

DisplayData(rs1)
jammus
+4  A: 

The other answers here are similar but ...

Sub DiplayData(rst)
   Response.Write rst.Source
End Sub
DisplayData rs1

Note Sub not Function since no value is returned. Also where you call a procedure as statement rather than as a function whose value you are assigning to a variable, do not enclose the parameters in ( ).

AnthonyWJones
Thanks to everyone for answering. I was almost there but needed just that little hint!!!
A: 

The quickest and dirtiest (and certainly the least secure) method of retrieving any variable by it's name is:

Function DisplayData(rsName)
   Dim localRS

   Set localRS = Eval(rsName)

   Response.Write localRS.Source
End Function

The above method really shouldn't be used if the value of rsName is retrieved from the web (via form, etc), as this gives scope for an injection attack on the ASP/ VBScript level.

However, given the limited number of available recordsets mentioned, the following would be more secure:

Function DisplayData(rsName)
   Dim localRS

   Select Case LCase(rsName) ' Case insensitive matches
   Case "rs1"
      Set localRS = rs1
   Case "rs2"
      Set localRS = rs2
   Case "rs3"
      Set localRS = rs3
   Case Else ' What to do if the name is not recognised
      Err.Raise 4000, "DisplayData", "Bad record set name"
   End Select

   Response.Write localRS.Source
End Function
Jason Musgrove