ASP - Using a Function Parameter to Reference a Recordset

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

I have a script that fetches data in 3 different recordsets. They are called rs1, rs2 and rs3.

I have a rather large piece of code later in the script, so I created a function to keep the save space, etc.

Inside the function, I want to use information from previously saved recordsets. I tried to pass the name of the Recordset to the function like this:

Function displayData(recordsetName)

response.write(recordsetName.Source)

End Function

displayData("rs1")

      

However, this is trying to show results from a recordset called recordsetName, and since that doesn't exist, it throws an error.

Someone told me to use "ByRef", however, this generates an error saying the recordset does not exist.

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

thank

+2


source to share


5 answers


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, when you call the procedure as a statement, and not as a function whose value you are assigning to a variable, do not include parameters in ().

+6


source


If you want it to print in the browser when you call it, then you should use Sub

like this:

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 must be Sub since it returns nothing.



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

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

Dim rs1 as new Recordset
'snip
displayData (rs1)

      

Don't try to understand why you are trying to do it any other way.

+1


source


You cannot pass the name of the Recordset, which you must pass to the Recordset object.

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

      

I also changed your function to Sub, because in VB the function should return something.

0


source


If you really wanted to convey a name you could do

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

DisplayData("rs1")

      

But don't. This is stupid and can get you in trouble. You have to do it as the other guys talk and transmit in the recordset itself.

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

DisplayData(rs1)

      

0


source


The fastest and dirtiest (and by far the least secure) way to get any variable by name:

Function DisplayData(rsName)
   Dim localRS

   Set localRS = Eval(rsName)

   Response.Write localRS.Source
End Function

      

The above method should really not be used if the value rsName

is fetched from the internet (via a form, etc.), as it allows for an ASP / VBScript level injection attack.

However, given the limited number of recordsets available, 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

      

0


source







All Articles