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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now