|
This example uses the Append and CreateParameter methods to execute a stored procedure with input parameters.
Public Sub AppendX()
Dim cnn1 As ADODB.Connection
Dim cmdByRoyalty As ADODB.Command
Dim prmByRoyalty As ADODB.Parameter
Dim rstByRoyalty As ADODB.Recordset
Dim rstAuthors As ADODB.Recordset
Dim intRoyalty As Integer
Dim strAuthorID As String
Dim strCnn As String
'Open the connection.
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;"&_
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=;"
cnn1.Open strCnn
cnn1.CursorLocation = adUseClient
'Open the command object with a single parameter.
Set cmdByRoyalty = New ADODB.Command
cmdByRoyalty.CommandText = "byroyalty"
cmdByRoyalty.CommandType = adCmdStoredProc
'Get parameter values and append parameters.
intRoyalty = Trim(InputBox("Enter royalty:"))
Set prmByRoyalty = cmdByRoyalty.CreateParameter("percentage", _
adInteger, adParamInput)
cmdByRoyalty.Parameters.Append prmByRoyalty
prmByRoyalty.Value = intRoyalty
'Create a record set by executing the command.
Set cmdByRoyalty.ActiveConnection = cnn1
Set rstByRoyalty = cmdByRoyalty.Execute
'Open the Authors table to get the author's name for display.
Set rstAuthors = New ADODB.Recordset
rstAuthors.Open "authors", cnn1,,, adCmdTable
'Print the current data in the record set and add the author's name from the Authors table.
Debug.Print "Authors with "&intRoyalty&" percent royalty"
Do While Not rstByRoyalty.EOF
strAuthorID = rstByRoyalty!au_id
Debug.Print ""&rstByRoyalty!au_id&", ";
rstAuthors.Filter = "au_id ='"&strAuthorID&"'"
Debug.Print rstAuthors!au_fname&""&rstAuthors!au_lname
rstByRoyalty.MoveNext
Loop
rstByRoyalty.Close
rstAuthors.Close
cnn1.Close
End Sub |
|