Solved

Getting Varchar Param Value From Stored Proc in SQL 7.0

Posted on 2000-05-01
6
382 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

831 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