Experts,
I am trying to automatically import some data generated by a SQL Server stored procedure into Excel, from code. It kind of works, but the problem I am running into is that I can't seem to get the parameters to work like they should (in my mind). The code is actually executed on the webserver (javascript), but, I can replicate the exact behaviour in Excel VBA itself so I will use VBA as an example:
Dim objTable As QueryTable
objTable = ActiveSheet.QueryTables.Add(strConnectionString,
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(1, 1))
objTable.CommandText = "MyProcedure " & lngSomeValue & ", " & lngSomeOtherValue
objTable.Refresh True
This in itself works fine. However, given my situation, the values given to the SP are from user input and, well, SQL injection is a thing to worry about. So, I would rather do something like:
objTable = ActiveSheet.QueryTables.Add(strConnectionString,
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(1, 1))
objTable.CommandText = "MyProcedure"
objTable.Parameters.Add("MyParam").SetParam 1, lngSomeValue
objTable.Parameters.Add("MySecondParam").SetParam 1, lngSomeOtherValue
objTable.Refresh True
...Which I can't get to work. How can I get the QueryTable to work while giving up individual parameters?
(I'm sorry if this is unclear - It feels kind of hard to explain)
I'm not sure you can achieve your second goal using QueryTables (would be interested to be proven wrong though!) - you might have to look at using ADO and the CopyFromRecordset method instead.
Regards,
Rory