469,923 Members | 1,777 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Returning A Value from a single function

HI,

I would like to know if I can some set a variable to return a single value
from a function using the set command. Is this possible?
Thanks in advance for your answer

Sean

SET test = ReturnValue(OptionID)

Function ReturnValue(ByVal OptionID)

Set adoCmd = Server.CreateObject("ADODB.Command")
adoCmd.ActiveConnection = db_conn
adoCmd.CommandType = adCmdStoredProc
adoCmd.CommandText = "sp_findoption"
adoCmd.Parameters.Append adoCmd.CreateParameter("pOptionID",adInteger)
adoCmd.Parameters("pOptionID") = OptionID
Set adoRec = adoCmd.Execute()
OptionValue = adoRec(1)
adoRec.close Set
adoRec = Nothing
Set adoCmd = Nothing

End Function
Jul 19 '05 #1
2 4501
Have you tried it? Should be fine, although if you're just returning a
value from a recordset, which will be a string or a numeric, don't use SET.
Just do:

test = ReturnValue(OptionID)

Ray at home

"sean" <se********@shopsmart.com.au> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
HI,

I would like to know if I can some set a variable to return a single value from a function using the set command. Is this possible?
Thanks in advance for your answer

Sean

SET test = ReturnValue(OptionID)

Function ReturnValue(ByVal OptionID)

Set adoCmd = Server.CreateObject("ADODB.Command")
adoCmd.ActiveConnection = db_conn
adoCmd.CommandType = adCmdStoredProc
adoCmd.CommandText = "sp_findoption"
adoCmd.Parameters.Append adoCmd.CreateParameter("pOptionID",adInteger)
adoCmd.Parameters("pOptionID") = OptionID
Set adoRec = adoCmd.Execute()
OptionValue = adoRec(1)
adoRec.close Set
adoRec = Nothing
Set adoCmd = Nothing

End Function

Jul 19 '05 #2
sean wrote:
HI,

I would like to know if I can some set a variable to return a single
value from a function using the set command. Is this possible?
Thanks in advance for your answer

Sean

SET test = ReturnValue(OptionID)
Unless you are intending to return an object from your function, do not use
the "SET" keyword.
Function ReturnValue(ByVal OptionID)

Set adoCmd = Server.CreateObject("ADODB.Command")
adoCmd.ActiveConnection = db_conn
adoCmd.CommandType = adCmdStoredProc
adoCmd.CommandText = "sp_findoption"
adoCmd.Parameters.Append adoCmd.CreateParameter("pOptionID",adInteger)
adoCmd.Parameters("pOptionID") = OptionID
Set adoRec = adoCmd.Execute()
OptionValue = adoRec(1)
adoRec.close Set
adoRec = Nothing
Set adoCmd = Nothing

End Function


This "function" does not return anything. To return a value from a function,
you assign the value you wish to return to the name of the function within
the function code. I'm guessing that you want to return the value contained
in the second field of the recordset returned by the stored procedure -
adoRec(1) - which makes me wonder why your stored procedure is returning
more than one field ...

Anyways, to return that value, do this instead:
ReturnValue = adoRec(1)

HTH,
Bob Barrows
PS. I would consider using an output parameter in your stored procedure to
return that single value. Using a recordset is overkill

--
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"
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Ken Godee | last post: by
7 posts views Thread by confusedcoder | last post: by
41 posts views Thread by Materialised | last post: by
28 posts views Thread by Yevgen Muntyan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.