I have the following stored procedure in SQL Server:
DECLARE @return_value int,
@S int
EXEC @return_value = [dbo].[SP_TestParams]
@L1 = 2,
@L2 = 2,
@S = @S OUTPUT
SELECT @S as N'@S'
SELECT 'Return Value' = @return_value
Procedure executes the code S = L1 + L2
.
Return code returns the value depending of the S
value:
IF L1 is null or L2 is null SET ret_code = 0
ELSE IF S < 10 SET ret_code = 1
ELSE IF S >= 10 SET ret_code = 2
I'm using the following the VBA code to trigger the above procedure, asking for parameters L1
and L2
(in given question let's assume that both are equal 2) and want to display the both @S
and @return_value
in the msgbox, but I get an error that procedure or function has too many arguments.
Sub TriggerProcedure ()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Driver={SQL Server};Server=MY_DATABASE;Uid=MY_LOGIN;Pwd=MY_PASSWORD;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
cn.Open
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "dbo.SP_TestParams"
cmd.ActiveConnection = cn
Dim L1 As Integer, L2 As Integer, S As Integer, ret_code As Integer
L1 = InputBox("Please enter parameter L1:")
L2 = InputBox("Please enter parameter L2:")
cmd.Parameters.Append cmd.CreateParameter("@L1", adInteger, adParamInput, , L1)
cmd.Parameters.Append cmd.CreateParameter("@L2", adInteger, adParamInput, , L2)
cmd.Parameters.Append cmd.CreateParameter("@S", adInteger, adParamOutput)
cmd.Parameters.Append cmd.CreateParameter("@return_value", adInteger, adParamReturnValue)
cmd.Execute
S = cmd.Parameters("@S").Value
return_code = cmd.Parameters("@return_value").Value
ret_code = cmd.Parameters("@return_value").Value
MsgBox "Parametr S - sum" & L1 & " i " & L2 & " equal to" & S & vbNewLine & "Return code of the procedure is " & ret_code
cn.Close
End Sub
The VBA code should display the results of the two queries (S
& return_code
).