Oracle stored procedure call from VB6

Posted on 2003-11-24
Last Modified: 2008-02-01
Hi everebody,
Maybe somebody know link/book with good explanation/examples how to Open Connection and call Oracle's stored procedure with OUT parameters from VB6?

Thanks in advance,
Question by:IlyaFox
  • 3

Accepted Solution

Dang123 earned 250 total points
ID: 9813707
LVL 14

Assisted Solution

waty earned 250 total points
ID: 9830950
' #Mandix Repository#*****************************************************
' * Programmer Name  : Oracle
' * Web Site         :
' * E-Mail           :
' * Date             : 13/11/01
' **********************************************************************
' * Comments         : Calling Stored Procedures and Functions From VB/ADO using the OLEDB Provider
' *
' * This article contains an example of how to call stored procedures
' * and  functions from Visual Basic using Microsoft Activex Data
' * Objects (ADO)  and the Oracle Provider for OLEDB.
' *
' **********************************************************************
Private Sub Callproc_Click()
   'This code demonstrates calling an Oracle Packaged Procedure and Function
   ' using the ODBC {Call...} Syntax From Visual Basic using the Microsoft
   ' Activex Data Objects (ADO) 2.1 Interface via The Oracle OLEDB Provider.
   '  The pl/sql Package called is
   'create or replace package odbpack as
   'Procedure Proc(param1 in number,param2 in out number,
   '     param3 out number);
   'Function Func(param1 in varchar2, param2 in out varchar2,
   '    param3 out varchar2) return number;
   'end odbpack;
   'create or replace package body odbpack as
   'Procedure Proc(param1 in number,param2 in out number,
   '     param3 out number) is
   '      param2 := param1+param2;
   '      param3 := param1;
   'Function func(param1 in varchar2, param2 in out varchar2,
   '    param3 out varchar2) return number is
   '    param2 := param1||param2;
   '    param3 := param1;
   '    return length(param2);
   'end odbpack;
   ' This code was tested using VB 6 and the Version of the OLEDB Provider


   Dim cnn1             As ADODB.Connection
   Dim cmdExeproc       As ADODB.Command
   ' Open connection.
   Set cnn1 = New ADODB.Connection
   ' Modify the following line to reflect a Connection within your environment
   strCnn = "Provider=OraOLEDB.Oracle;User ID=SCOTT;Password=TIGER;Data Source=S692816.WORLD;"

   ' Create Parameter Objects to be used later

   Dim prm1             As ADODB.Parameter
   Dim prm2             As ADODB.Parameter
   Dim prm3             As ADODB.Parameter
   Dim prm4             As ADODB.Parameter

   cnn1.Open strCnn

   Set cmdExeproc = New ADODB.Command

   cmdExeproc.ActiveConnection = cnn1

   cmdExeproc.CommandText = "{call odbpack.proc(?,?,?)}"
   '  In the next set of code, we have to manually set up
   '  the parameters types since Our OLEDB Provider cannot provide parameter information
   '  when calling procedures

   Set prm1 = cmdExeproc.CreateParameter("prm1", adSmallInt, adParamInput, , 1)
   cmdExeproc.Parameters.Append prm1

   Set prm2 = cmdExeproc.CreateParameter("prm2", adSmallInt, adParamInputOutput, , 2)
   cmdExeproc.Parameters.Append prm2

   Set prm3 = cmdExeproc.CreateParameter("prm3", adSmallInt, adParamOutput)
   cmdExeproc.Parameters.Append prm3

   ' Now we have the parameters set - execute the command.


   ' Show the results

   MsgBox ("Return Values from Proc are : " & _
      cmdExeproc.Parameters(1).Value & _
      " and " & _

   ' Now do the same thing for a function

   Set cmdExeproc = New ADODB.Command

   cmdExeproc.ActiveConnection = cnn1

   '  Set up the command text - note the ? = call syntax
   cmdExeproc.CommandText = "{? = call odbpack.func(?,?,?)}"

   '  In the next set of code, we have to manually create
   '  the parameters since our OLEDB driver cannot provide this
   '  information when calling packaged procedures.

   ' first parameter is the return value - note it's type

   Set prm1 = cmdExeproc.CreateParameter("prm1", adInteger, adParamReturnValue)
   cmdExeproc.Parameters.Append prm1

   Set prm2 = cmdExeproc.CreateParameter("prm2", adVarChar, adParamInput, 3, "Odd")
   cmdExeproc.Parameters.Append prm2

   Set prm3 = cmdExeproc.CreateParameter("prm3", adVarChar, adParamInputOutput, 30, "Fred")
   cmdExeproc.Parameters.Append prm3

   Set prm4 = cmdExeproc.CreateParameter("prm4", adVarChar, adParamOutput, 30)
   cmdExeproc.Parameters.Append prm4

   ' execute the command


   'Show the results
   MsgBox ("Return Values from Func are : " & _
      cmdExeproc.Parameters(0).Value & _
      " and " & _
      cmdExeproc.Parameters(2).Value & _
      " and " & _

   '  Close Connection
End Sub
LVL 14

Expert Comment

ID: 9830951
' #Mandix Repository#*****************************************************
' * Programmer Name  : Yuening Dai
' * Web Site         :
' * E-Mail           :
' * Date             : 08/12/1999
' * Time             : 15:53
' **********************************************************************
' * Comments         : Pass parameters to stored procedures of Oracle using ADO
' *
' *
' **********************************************************************
'From MSDN online help sample, if you want to pass parameters to a stored
'procedure, you need to create parameter objects, append them to ADO Command
'object, and then assign the values. But I found an easy way to do the same
'work if you are going to call procedures in Oracle. In the same method, you can
'return from an Oracle PL/SQl function.
Dim strSQL As String
Dim qryStoredProc As New ADODB.Command
Dim id As Long
Dim name As String
With qryStoredProc
   .CommandText = "scott.instrec"
   .CommandType = adCmdStoredProc
   .ActiveConnection = mCnn          'Suppose that you already have a valid one.
End With
id = CLng(InputBox("Enter an integer below:"))
name = InputBox("Enter the  name:")
'Insert a new record into Oracle table.
qryStoredProc(0) = id
qryStoredProc(1) = name
'The first parameter is InOut and the other is Out.
MsgBox "ParamInOut: " & qryStoredProc(0) & _
   NL & "ParamOut: " & qryStoredProc(2)

'The Oracle table MYTEST has only two cols: MYID and MYTEST
'The code for the stored procedure is listed below.
'procedure       instrec (
' p_id in out number, p_name in varchar2,
' p_id_plus out number
' ) AS
' insert into mytest values(p_id, p_name);
' commit;
' -- Testing only
' p_id_plus:=p_id+1;
' p_id:=p_id+1;
'END instrec;
LVL 14

Expert Comment

ID: 9830953
' #Mandix Repository#*****************************************************
' * Programmer Name  : Support
' * Web Site         :
' * E-Mail           :
' * Date             : 12/10/01
' **********************************************************************
' * Comments         : Insert data in Oracle using a Stored Procedure
' *
' * Very basic sample of how to use a text box to get input from
' * a user and then insert it into Oracle using a  stored procedure.
' *
' **********************************************************************

' Sql Script to create the table and Stored procedure:
' create table jo
' (no number,
' name char(10),
' lname char(10));
' create or replace procedure Getjo
'    (no in number,name in char, lname in char)
'  as
'  begin
'   insert into jo values (no + 1 , name ,lname);
'  end;
' Commit;

Private Sub Command1_Click()

   Dim con              As New ADODB.Connection
   Dim cmd              As New ADODB.Command
   Dim Param1           As New ADODB.Parameter
   Dim Param2           As New ADODB.Parameter
   Dim Param3           As New ADODB.Parameter

   con.ConnectionString = "DSN=jillbert; UID=scott; PWD=tiger;"

   cmd.ActiveConnection = con
   cmd.CommandText = "Getjo"
   cmd.CommandType = adCmdStoredProc

   Set Param1 = cmd.CreateParameter("Prm1", adInteger, adParamInput, , 100)
   cmd.Parameters.Append Param1

   Set Param2 = cmd.CreateParameter("Prm2", adChar, adParamInput, 10, Text1.Text)

   cmd.Parameters.Append Param2

   Set Param3 = cmd.CreateParameter("Prm3", adChar, adParamInput, 10, Text2.Text)
   cmd.Parameters.Append Param3

   MsgBox ("Done Executing the Stored Procedure")
End Sub

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

791 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