问题
I have a SQL SP with the following parameters...
myProc(@param1 AS TINYINT, @sparam2 AS TINYINT, @param3 AS TINYINT, @param4 AS
TINYINT, @param5 AS TINYINT, @param6 AS TINYINT, @param7 AS TINYINT, @paramOut
NVARCHAR (255) OUTPUT)
and I have the following ADODB code in VBA...
sVal1 = 1
sVal2 = 1
sVal3 = 1
sVal4 = 1
sVal5 = 1
sVal6 = 1
sVal7 = 1
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim param1, param2, param3, param4, param5, param6, param7, param8 As
ADODB.Parameter
Dim Rs As ADODB.Recordset
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DRIVER=SQL
Server;SERVER=SERVER;DATABASE=PEER_DB;Trusted_Connection=Yes"
cnn.Open cnn.ConnectionString
Set cmd = New ADODB.Command
Set Rs = New ADODB.Recordset
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "myProc"
Set param1 = cmd.CreateParameter("@param1", adTinyInt, adParamInput, 1, sVal1)
cmd.Parameters.Append param1
Set param2 = cmd.CreateParameter("@param2", adTinyInt, adParamInput, 1, sVal2)
cmd.Parameters.Append param2
Set param3 = cmd.CreateParameter("@param3", adTinyInt, adParamInput, 1, sVal3)
cmd.Parameters.Append param3
Set param4 = cmd.CreateParameter("@param4", adTinyInt, adParamInput, 1, sVal4)
cmd.Parameters.Append param4
Set param5 = cmd.CreateParameter("@param5", adTinyInt, adParamInput, 1, sVal5)
cmd.Parameters.Append param5
Set param6 = cmd.CreateParameter("@param6", adTinyInt, adParamInput, 1, sVal6)
cmd.Parameters.Append param6
Set param7 = cmd.CreateParameter("@param7", adTinyInt, adParamInput, 1,
adParamReturnValue)
cmd.Parameters.Append param7
Set param8 = cmd.CreateParameter("@paramOut", adVarChar, adParamOutput, 255,
adParamReturnValue)
cmd.Parameters.Append param8
Rs.CursorType = adOpenStatic
Rs.CursorLocation = adUseClient
Rs.LockType = adLockOptimistic
cmd.Execute
Rs.Open cmd
All I am attempting to do is pass in my VBA sVal variables and execute my procedure based on those parameters and then I need to return the output parameter which has been working; The procedure has not been executing properly when using the ADODB connection as I am not getting the results I should be. Can someone steer me in the right direction, am I missing some execute statement?
回答1:
Instead of:
cmd.Execute
Rs.Open cmd
Try
Set rs = cmd.Execute
For a detailed example, see How To Invoke a Stored Procedure with ADO Query Using VBA/C++/Java.
来源:https://stackoverflow.com/questions/20546265/adodb-not-passing-in-parameters