Hi Sara,
You wouldn't need necessarily need an SQL Server user defined function to return this, an output parameter in a stored procedure could return what you require in this circumstance.
If all you are asking is for is a single return value to be assigned to a variable in the client application then passing in your @Param1 parameter via the command object to a stored procedure and returning the variable @Code as an
Output parameter to your 'Access' application would give you your required result.
-
-
CREATE PROCEDURE dbo.usp_GetCode
-
(@Param1 int,@Code varchar(8) Output)
-
SELECT @Code = CODE_COL
-
from T_TABLE
-
WHERE ID_COL = @Param1
-
- Return the result in the output parameter
-
SELECT @Code
-
GO
-
This is how you might then call it from your code in Access
in line with your posted variable
-
-
Dim cmd As ADODB.Command
-
Dim par As ADODB.Parameter
-
Dim strSomeVariable as String
-
Set cmd = New ADODB.Command
-
cmd.ActiveConnection = CurrentProject.Connection
-
cmd.CommandText = "dbo.usp_GetCode"
-
cmd.CommandType = adCmdStoredProc
-
-
'Define the input and output variables and append them to the collection
-
Set par = cmd.CreateParameter("@Param1", adVarChar, adParamInput, 8)
-
cmd.Parameters.Append par
-
Set par = cmd.CreateParameter("@Code", adInteger, adParamOutput)
-
cmd.Parameters.Append par
-
-
'we can assign the value for the input variable to be passed to SQL Server by reference to an access screen controls currently displayed value for instance.
-
-
cmd.Parameters("@Param1") = Forms!frmMyFormName!MyControlName
-
-
cmd.Execute
-
-
'and then assign the stored procedure return value to the access variable
-
-
strSomeVariable = cmd.Parameters("@Code").Value
-
As an aside ...user defined functions certainly have their place but are not visually exposed within the graphical user interface (GUI) of an Access ADP file whereas stored procedures are, you can see, and edit the relevant syntax.
Trust this helps you,
Regards
Jim :)