Avatar of Amanda Walshaw
Amanda Walshaw
Flag for Australia asked on

running stored procedure from excel office 2010

How would you run a stored procedure from excel 2010.
Microsoft SQL Server 2008Microsoft Excel

Avatar of undefined
Last Comment
nutsch

8/22/2022 - Mon
nutsch

Like this?

Sub Stufftodo

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String, objField As ADODB.Field
Dim strRst As String
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
 
strConn = "PROVIDER=SQLOLEDB;" & _
            "DATA SOURCE=DCC08SQL;INITIAL CATALOG=CAPEX;" & _
            " INTEGRATED SECURITY=sspi;"

Set conn = New ADODB.Connection
conn.ConnectionString = strConn
conn.Open

with activesheet
        
        cmd.ActiveConnection = conn
        cmd.CommandText = "spRetrievePivotByProject"
        cmd.CommandType = adCmdStoredProc
        
        cmd.Parameters.Refresh
        cmd.Parameters("@year").Value = lYearLoop
        cmd.Parameters("@projnum").Value = .Range("summary_ProjNum")
         
        Set rs = cmd.Execute()
        
        If Not rs.EOF Then
        
            .Cells(2, 2).CopyFromRecordset rs
        
            For Each objField In rs.Fields
                .Cells(1, lLoop) objField.Name
            Next
            
        End If 'Not rs.EOF
 end with

   
conn.Close
Set rs = Nothing
Set conn = Nothing

End Sub

Open in new window

Amanda Walshaw

ASKER
IS THAT DONE AS A VB SCRIPT OR THROUGH Data connection wizard, I am using Excel 2010
ASKER CERTIFIED SOLUTION
nutsch

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes