Accessing an SQL Server stored procedure (containing no SELECT statement) from Excel VBA, ideally without using Excel Data Connections?
I'm trying to build a VBA form that runs a SQL Server stored procedure to cache data before it does anything else. Ideally I'd prefer not to use an Excel data connection because then I can't export it to a new workbook with a minimum of fuss - it'd be nice to contain all of the information in the form's file.
The main way I know to connect VBA to a database is using code along these lines:
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open ConnectionString
rst.Open QueryText, cnn, adOpenDynamic
/*Whatever you're doing to the data goes here*/
rst.Close
cnn.Close
The problem I'm running into is that the caching proc doesn't return a dataset (not even a blank one), and this ty开发者_StackOverflow中文版pe of connection seems to throw a tizzy if there is no data returned. And I'd prefer not to modify the proc if I don't have to.
Is there a feasible solution, within these constraints? Or am I just being too whiny and should suck it up and use an Excel data connection or something?
I think you are looking for an ADODB.Command. Try something like this:
Dim cnn As New ADODB.Connection
Dim cmd As ADODB.Command
cnn.Open ConnectionString
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cnn
.CommandText = "EXEC spNameHere param1"
.CommandType = adCmdText
.Execute
End With
精彩评论