How to execute SQL Server store procedure from MS Access or VB

  • Thread starter Thread starter Guest
  • Start date Start date
The following code should help you in running stored procedure from VBA:

Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection

Dim cmd As ADODB.Command
Set cmd = New ADODB.Command

' If procedurename is "Proc" then

With cmd
Set .ActiveConnection = cnn

.CommandText = "Proc"
.CommandType = adCmdStoredProc

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

With rst
.CursorLocation = adUseClient
.Open cmd, , adOpenStatic, adLockReadOnly, adCmdStoredProc
End With
End With

Use the above method if your stored procedure returns a resultset.
For non-resultset returning stored Procedure,(eg. INSERT query) you may use
Execute method of command object to execute the procedure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top