Reputation:
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
Upvotes: 2
Views: 7624
Reputation: 36832
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.
Upvotes: 1
Reputation: 3583
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
Upvotes: 0
Reputation: 189457
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 ( ).
Upvotes: 6
Reputation: 2550
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)
Upvotes: 0
Reputation: 21078
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.
Upvotes: 0