Append and CreateParameter Methods Example
This example uses either the Append and CreateParameter methods to execute a stored procedure with an input parameter.
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 connection.
Set cnn1 = New ADODB.Connection
strCnn = "driver={SQL Server};server=srv;" & _
"uid=sa;pwd=;database=pubs"
cnn1.Open strCnn
' Open command object with one parameter.
Set cmdByRoyalty = New ADODB.Command
cmdByRoyalty.CommandText = "byroyalty"
cmdByRoyalty.CommandType = adCmdStoredProc
' Get paramater value and append parameter.
intRoyalty = Trim(InputBox("Enter royalty:"))
Set prmByRoyalty = cmdByRoyalty.CreateParameter("percentage", _
adInteger, adParamInput)
prmByRoyalty.Value = intRoyalty
cmdByRoyalty.Parameters.Append prmByRoyalty
' Create recordset by executing the command.
cmdByRoyalty.ActiveConnection = cnn1
Set rstByRoyalty = cmdByRoyalty.Execute
' Open the Authors table to get author names for display.
Set rstAuthors = New ADODB.Recordset
rstAuthors.Open "authors", cnn1
' Print current data in the recordset, adding
' author names from Authors table.
With rstByRoyalty
Debug.Print "Authors with " & intRoyalty & " percent royalty"
Do While Not .EOF
strAuthorID = !au_id
Debug.Print " " & !au_id & ", ";
With rstAuthors
.Filter = "au_id = '" & strAuthorID & "'"
Debug.Print !au_fname & " " & !au_lname
End With
.MoveNext
Loop
.Close
End With
rstAuthors.Close
cnn1.Close
End Sub
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.