Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Parameters in ADO Stored Procedures

Database

Using Parameters in ADO Stored Procedures

by  Norris68  Posted    (Edited  )
I have seen the same question about 3 times in a day, so here is a way of using parameters in stored procedures. The example has one input parameter and one output parameter and uses a stored procedure. Change the .CommandType to use it for queries. By the way, a Long variable is usually type adInteger.

Code:
Public Function GetCompanyName(InCID As Long) As String
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command

Dim prmInput As ADODB.Parameter
Dim prmOutput As ADODB.Parameter
' Set up a command object for the stored procedure.
With cmd
    Set .ActiveConnection = Conn1 'Conn1 is previously defined & open
    .CommandText = "get_company_name_from_id"
    .CommandType = adCmdStoredProc
    .CommandTimeout = 15
    Set prmInput = .CreateParameter("@cid", adInteger, adParamInput, , InCID)
    Set prmOutput = .CreateParameter("@cname", adVarWChar, adParamOutput, 255)
    .Parameters.Append prmInput
    .Parameters.Append prmOutput
    .Execute
    GetCompanyName = .Parameters(1).Value 'Zero-based array
End With
Set cmd = Nothing
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top