ActiveConnection, CommandText, CommandTimeout, CommandType, Size and Direction Properties Example

This example uses the ActiveConnection, CommandText, CommandTimeout, CommandType, Size, and Direction properties to execute a stored procedure.

Public Sub ActiveConnectionX()

	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

	' Define a command object for a stored procedure.
	strCnn = "driver={SQL Server};server=srv;" & _
		"uid=sa;pwd=;database=pubs"
	Set cmdByRoyalty = New ADODB.Command
	cmdByRoyalty.ActiveConnection = strCnn
	cmdByRoyalty.CommandText = "byroyalty"
	cmdByRoyalty.CommandType = adCmdStoredProc
	cmdByRoyalty.CommandTimeout = 15
		
	' Define the stored procedure's input parameter.
	intRoyalty = Trim(InputBox( _
		"Enter royalty:"))
	Set prmByRoyalty = New ADODB.Parameter
	prmByRoyalty.Type = adInteger
	prmByRoyalty.Size = 3
	prmByRoyalty.Direction = adParamInput
	prmByRoyalty.Value = intRoyalty
	cmdByRoyalty.Parameters.Append prmByRoyalty
  
	' Create a recordset by executing the command.
	Set rstByRoyalty = cmdByRoyalty.Execute
		
	' Open the Authors table to get author names for display.
	Set rstAuthors = New ADODB.Recordset
	rstAuthors.Open "authors", strCnn
	
	' 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
	
End Sub

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.