Reputation: 17258
I am attempting to create my ADODBcommand and parameters in the calligng code, so that I can pass them into a generic SQL function, but I get an error when retrieving the parameter from the list:
Dim prm As ADODB.Parameter
Dim cmd As ADODB.Command
Dim ParameterList(0) As Variant
Value = Cells(3,3).Value
Set cmd = New ADODB.Command
Set prm = cmd.CreateParameter("@Field", adVarChar, adParamInput, 50)
cmd.Parameters.Append prm
cmd.Parameters("@Field").Value = Value
ParameterList(0) = prm
Call SQLFunc(Data, SQLStr, adCmdStoredProc, cmd, ParameterList)
.
.
sub SQLFunc(ByRef Data as Variant, SQLStr as String, CommandType As ADODB.CommandTypeEnum, ByRef cmd As ADODB.Command, ParamList As Variant)
.
.
.
If cmd.CommandType <> adCmdStoredProc Then
cmd.Execute
Else
'Append the parameters
For i = LBound(ParamList, 1) To UBound(ParamList, 1)
Dim TempParam As ADODB.Parameter
Set TempParam = ParamList(i)
cmd.Parameters.Append TempParam 'Error here
Next
Set Rst = cmd.Execute
Could anyone please advise how I can achieve this?
Upvotes: 1
Views: 2803
Reputation: 175876
After
ParameterList(0) = prm
ParameterList(0)
is Empty
, to resolve this;
set ParameterList(0) = prm
I would also strongly type this so instead of variant use Dim ParameterList(0) As ADODB.Parameter
The error you see occurs because you already append the parameter to the command before calling SQLFunc
, in which you illegaly attempt to do so again. Parameter names must be unique when bound to a command.
You could remove the binding and use;
Set prm = cmd.CreateParameter("@Field", adVarChar, adParamInput, 50)
prm.Value = Value
Then prm
remains independent of cmd
.
Upvotes: 3