I have a VBA function that will execute parameterized SQL.
Public Function Execute(connection As ADODB.connection, ByVal sql As String, ParamArray parameterValues()) As ADODB.Recordset
'Returns a connected ADODB.Recordset that contains the results of the specified parameterized query.
Dim parameters() As Variant
parameters = parameterValues
Set Execute = ExecuteInternal(connection, sql, parameters)
End Function
The number of parameters can vary at runtime, and I construct the sql string dynamically using a selected range. An example sql string is:
DELETE FROM my_table WHERE id NOT IN (?, ?, ?)
I wish to pass the values in the range to the function also, but of course the naive approach of passing the range.value to the Execute function doesn't cause the range to be unpacked.
With New SqlCommand
Set rs = .Execute(con, sql, myRange.value)
End With
How can I unpack the variant into the ParamArray? I'm basically looking for the Python *
unpack operator.
Forgive me if it's a stupid question, but VBA is not my primary language.
Set rs = .Execute(con, sql, myRange1.Value, myRange2.Value, myRange3.Value)
– Excel Hero