Calling a SQL server stored procedure from VBA

Awaiz

New Member
Joined
Apr 3, 2015
Messages
19
Hi I am trying to call a stored procedure from VBA, my code is as below:



' SQL Connection

Dim conn As ADODB.Connection
Dim cmd As ADODB.Command

Set conn = New ADODB.Connection

conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=REVGOV;Data Source=pdlfslab10"


''''''''''''''''''Some of my parameters from excel goes here which I use as variables in the command text''''''''''''''


conn.Open


Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "exec MyProc '& FY &', '& Month &', '& VerticalSrNo &','& Vertical &','& LocationSrNo &','& Location &', '& UserName &'"


cmd.Execute
conn.Close

Set conn = Nothing
Set cmd = Nothing



End Sub




But I am getting an error "Syntax Error or Access Violation"


Help Needed!!!!!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Your command text is evaluating to this:
exec MyProc '& FY &', '& Month &', '& VerticalSrNo &','& Vertical &','& LocationSrNo &','& Location &', '& UserName &'

Did you intend to leave the & symbols within the single quotes?


I have some similar macros, where VBA calls on a SQL stored procedure. If your having trouble feel free to modify the code below (substitute the server name, db name, sheet references, variables - the example only has 3 variables getting passed, etc). If you havn't already done so you'll need to enable a MS ActiveX Object library under VBA Tools->preferences.

Code:
Sub exportHostData()
       
    Dim cn As ADODB.Connection
    Dim strSQL As String
    Set cn = New ADODB.Connection
    cn.CommandTimeout = 120

'clear old data
    Worksheets("Data").Range("A2:DM50000").ClearContents
    
    
    cn.Open "Provider=SQLOLEDB;Data Source=servername;" & _
     "Initial Catalog=databasename;Integrated Security = SSPI"
     
     ' Create a recordset object.
    Dim rsPubs As ADODB.Recordset
    Set rsPubs = New ADODB.Recordset

     Dim Company As Variant
     Dim Start_Date As Date
     Dim End_Date As Date
          
     Company = Worksheets("Company Scorecard").Range("B2").Value
     Start_Date = Worksheets("Company Scorecard").Range("Q2").Value
     End_Date = Worksheets("Company Scorecard").Range("Q4").Value + 1
              
     With rsPubs
     .ActiveConnection = cn
      .Open "exec spg_CompanyScorecard_HostUptimes @Company ='" & Company & "', @Start_Date ='" & Start_Date & "', @End_Date ='" & End_Date & "'"
      Sheet2.Range("C2").CopyFromRecordset rsPubs
      .Close
     End With

    Set rsPubs = Nothing
    Set cnPubs = Nothing
    
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top