Solved

Getting Varchar Param Value From Stored Proc in SQL 7.0

Posted on 2000-05-01
6
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 131
How to debug this code 7 65
vb6 connector to mongodb 2 166
Export PDF Form fields to Access  or Excel  in Tab order 16 129
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…

710 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