Running sql stored procedures from Excel

  • Thread starter in-over-his-head-bill
  • Start date
I

in-over-his-head-bill

I am trying to right a simple front-end in VBA for Excel to return results
from SQL stored procedures after the user selects the input variable values
from a multi-select list box.

I did something similar out of Access last year; but all the ADODB. syntax
doesn't work in Excel VBA. (I've figured out the differences between Access
and Excel with the list box properties but not with the connection, query,
parameters... )

Provided below is my Access VBA code; how do I need to modify to run out of
Excel?

I am running Access 2002 and Excel 2002.


Private Sub lst_user_DblClick(Cancel As Integer)

'Get user-selected value for stored procedure parameter

For j = 0 To lst_user.ListCount - 1
If lst_user.Selected(j) = True Then
var1 = lst_user.ItemData(j)
End If
Next j

Dim cnn1 As ADODB.Connection
Dim runspcmd As ADODB.Command
Dim runempty As ADODB.Command
Dim runsp_param As ADODB.Parameter
Dim intvar As Integer
Dim strCnn As String

Set cnn1 = New ADODB.Connection
cnn1.ConnectionString = "Provider=SQLOLEDB;driver={SQL
Server};Server=(local);Initial Catalog=excelsqltest;
userid=myname;Password=;Trusted_Connection=Yes"
cnn1.Open strCnn

' Open command object with one parameter.
Set runspcmd = New ADODB.Command
runspcmd.CommandText = "sp_testproc"
runspcmd.CommandType = adCmdStoredProc


' Get parameter value and append parameter.
intvar = var1
Set runsp_param = runspcmd.CreateParameter("recno", _
adInteger, adParamInput)
runspcmd.Parameters.Append runsp_param
runsp_param.Value = intvar


' execute the command.
Set runspcmd.ActiveConnection = cnn1
runspcmd.Execute

cnn1.Close

DoCmd.Close acForm, Form.Name
DoCmd.OpenReport "outputtable", acViewPreview

end sub
 

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

Top