Solved

Getting Varchar Param Value From Stored Proc in SQL 7.0

Posted on 2000-05-01
6
383 Views
Last Modified: 2013-12-25
The output parameter in Stored Proc is defined as Varchar. After executing the stored proc, "@Two", the output parameter contains "" instead of the actual returned value. I am including the source here:

CREATE PROCEDURE TestProc
    @One As Integer,
    @Two As  varchar(20)  Output
As

Select @Two = 'johnny'

VB Class:

Option Explicit

Public sDSN As String
Public sUserID As String
Public sPW As String
Public sStoredProcName As String

Private m_oConnection As ADODB.Connection
Private m_oCommand As New ADODB.Command

Private Sub Class_Initialize()
 
  Set m_oConnection = New ADODB.Connection
  Set m_oCommand = New ADODB.Command
 
  sDSN = ""
  sUserID = ""
  sPW = ""
  sStoredProcName = ""

End Sub

Private Sub Class_Terminate()
 
  Set m_oConnection = Nothing
  Set m_oCommand = Nothing

End Sub

Public Sub InitializeConnection()

  m_oConnection.ConnectionString = "driver={SQL Server};" & _
        "server=JIMAM;uid=johnny;pwd=johnny;database=Test"

  m_oConnection.Open

  m_oCommand.CommandType = adCmdStoredProc
 
End Sub

Public Sub AddParam(ByVal ParamName As String, _
                         ByVal ParamType As DataTypeEnum, _
                         ByVal Direction As ParameterDirectionEnum, _
                         Optional ByVal Value As Variant)

  On Error GoTo ErrorHandler
 
  Dim Param As ADODB.Parameter
 
  Set Param = m_oCommand.CreateParameter(ParamName, ParamType, Direction, , Value)
 
  m_oCommand.Parameters.Append Param
 
  Set Param = Nothing
 
  Exit Sub

ErrorHandler:
    Set Param = Nothing
    Err.Raise Err.Number, , Err.Description
    Exit Sub
   
End Sub
                         
Public Sub AssignParamValue(ByVal ParamName As String, _
                                 ByVal Value As Variant)

  m_oCommand(ParamName) = Value
 
End Sub
                         
Public Function GetParamValue(ByVal ParamName As String) As Variant

  GetParamValue = m_oCommand(ParamName)

End Function
                         
Public Sub ExecuteProc()
 
  m_oCommand.CommandText = sStoredProcName
  m_oCommand.Execute
 
End Sub

Form Code:

 Private Sub Command1_Click()
  Dim x As New ABSQLStoredProc
 
  x.InitializeConnection
 
  x.sStoredProcName = "TestProc"
 
  x.AddParam "@One", adInteger, adParamInput, 1
  x.AddParam "@Two", adBSTR, adParamOutput
 
  x.ExecuteProc
 
  Command1.Caption = CStr(x.GetParamValue("@Two"))
 
  Set x = Nothing
 
End Sub

Please help.

Thanks,
Johnny
0
Comment
Question by:johnny_imam
  • 3
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2766227
Try adVarChar instead of adBSTR.
0
 

Author Comment

by:johnny_imam
ID: 2766288
When I define it as adVarChar, this exception is raised:

"The application has improperly defined a Parameter object."
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2766325
Use the Refresh method to see what data types that ADO thinks the parameters are.

Also, you should specify the size of the parameters.  Modify your code thus:


Public Sub AddParam(ByVal ParamName As String, _
                         ByVal ParamType As DataTypeEnum, _
                         ByVal Direction As ParameterDirectionEnum, _
                         ByVal Size as Integer, _
                         Optional ByVal Value As Variant)

  On Error GoTo ErrorHandler
   
  Dim Param As ADODB.Parameter
   
  Set Param = m_oCommand.CreateParameter(ParamName, ParamType, Direction, Size, Value)
   
  m_oCommand.Parameters.Append Param
   
  Set Param = Nothing
   
  Exit Sub

....

  x.AddParam "@One", adInteger, adParamInput, 8, 1
  x.AddParam "@Two", adVarChar, adParamOutput, 20

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:johnny_imam
ID: 2766341
bhess1,

Specifying the size did it...please go ahead answer the question so I can assign the grade.

Thanks,
Johnny
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 150 total points
ID: 2766381
You can accept a comment as an answer - just check the 'accept comment as answer' when reviewing the question.
0
 

Author Comment

by:johnny_imam
ID: 2766438
Thanks
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question