Email Us : sunilkumark11@gmail.com
Free Training > SQL PL / SQL Forms Reports Oracle Applications
< Previous Next >

 
Forms - Creating sub programs in forms

In the database, create a new table LOANS
Query

SQL> Create table loans ( cust_id              number(3),
                                                cname                  varchar2(20),
                                                principal               number(14,2 ),
                                                duration               number(3),
                                                interest                number(10,2) );

Open the form builder tool.
Create new form based on loans table using data block wizard and layout wizard.

Save the form with the name LOANS.fmb

In the Object navigator of LOANS.fmb, Select program units  Click on create button
Name --  simp_intr  , Select  type as function OK

Provide the following code and compile.

FUNCTION simp_intr ( p  number, n number, r number)
RETURN number
IS
sintr  number(13,3);
BEGIN
 
  sintr := ( p*n*r)/100;
  return (round ( sintr));
END;

Similarly create another program unit.

Name --  comp_intr  , Select  type as function OK

Provide the following code and compile.

 

FUNCTION comp_intr ( p  number, n number, r  number)
RETURN number
IS
ci  number(14,2);
BEGIN
                ci := p* power ( ( 1+r/100), n);
                return ( round ( ci ));
 
END;

Note
We have created two functions under program units.
One function to calculate simple interest.
Another function to calculate compund interest.

We can see the two functions which we have created in Object Navigator.

In the Layout editor, create a button and two display units.

For button, provide the following properties.
General
Name -- B1

Functional
Label -- Calculate

For the first display item, provide the following properties
General
Name -- D1
Prompt
Prompt -- Simple Interest

For the  second  display item, provide the following properties
General
Name -- D2
Prompt
Prompt -- Compound Interest

Create WHEN-BUTTON-PRESSED trigger for the button B1, and provide the following code and compile.

:D1 := simp_intr( :principal, :duration, :interest);

:D2 := comp_intr (:principal, :duration, :interest);

In the above code, D1 and D2 are display items.
So, When button is pressed, we are invoking functions simp_intr and comp_intr.
These functions will calculate the interest and populate it in display items.

Save the file.
Compile the file.
Run the file.

Provide the following values

Cust Id -- 1
Cname -- arun
Duration - 5
Principal -- 1000
Interest -- 10

Click on Calculate button.
We can see that Simple Interest and compound Interest values are displayed.

Note
The two functions simp_intr and comp_intr are created at form level, not at database level.

We can create function at database level and invoke it in form.

Let's look at that example.

Create function calc_intr  at database level
Code

create or replace function calc_intr ( p  number, n  number, r  number, si out number)
return number
is
ci  number(14, 2);
begin
si := round (  ( p*n*r)/100 );
ci := p* power (  ( 1+r/100) , n);
return ( round (ci) );
end;

In the above code, function returns simple interest using OUT parameter and returns compound interest using return statement.

 

Open the form builder tool.
Create new form based on loans table using data block wizard and layout wizard.

Save the form with the name LOANS2.fmb

In the Layout editor, create a button and two display units.

For button, provide the following properties.
General
Name -- B1

Functional
Label -- Calculate

For the first display item, provide the following properties
General
Name -- D1
Prompt
Prompt -- Simple Interest

For the  second  display item, provide the following properties
General
Name -- D2
Prompt
Prompt -- Compound Interest

Create WHEN-BUTTON-PRESSED trigger for the button B1, and provide the following code and compile.

:D2 := calc_intr (:principal , :duration, :interest, :D1);

Save the file.
Compile the file.
Run the file.

Provide the following values

Cust Id -- 1
Cname -- arun
Duration - 5
Principal -- 1000
Interest -- 10

Click on Calculate button.

We can see that Simple Interest and compound Interest values are displayed.

Note
In this example, When Calculate button is pressed, we are invoking function calc_intr.

The Function calc_intr is created at database level, not at form level.

Let's have another example

Create the following package at database level

Create or replace package epack
is
function gross ( basic  number) return  number;
function bonus_cal ( vjob  varchar2 ) return number;
end epack;
/

Create or replace package body epack
is
function gross ( basic number ) return number
is
gpay number(16,2);
begin
gpay :=  basic + basic* 0.25 + basic *0.35 - basic *0.12 ;
return ( round ( gpay ) );
end gross;

function bonus_cal ( vjob  varchar2 ) return number
is
bonus  number(6);
begin
if vjob = 'CLERK' then
bonus := 3000;
elsif vjob = 'SALESMAN' then
bonus := 3500;
elsif vjob ='MANAGER' then
bonus := 4000;
else
bonus := 5000;
end if;

return ( bonus);
end bonus_cal;

end epack;

Under INFO block , Create two  display  items.

For the first display item, provide the following properties.

General
Name -- D1
Item type  -- Display Item
Database
Database Item --  No
Physical
Canvas  -- CANVAS4  ( My canvas name which is created by wizard )
Prompt
Prompt -- Gross sal

For the second display item, provide the following properties.

General
Name -- D2
Item type  -- Display Item
Database
Database Item --  No
Physical
Canvas  -- CANVAS4  ( My canvas name which is created by wizard )
Prompt
Prompt -- Bonus

For both the display units as we have mentioned canvas property as CANVAS4,
they will appear in Layout Editor.

Under datablock EMPNINE, create POST-QUERY trigger.
Provide the following code and compile.

:D1 := epack.gross(:SAL);

:D2 := epack.bonus_cal (:JOB);

Save the form.
Compile the form.
Run the form.
Click on execute query
We can see that gross sal and bonus is calculated and displayed by using POST-QUERY trigger.

 


< Previous Next >