Solved

Getting Varchar Param Value From Stored Proc in SQL 7.0

Posted on 2000-05-01
6
380 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 69

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now