Example Oracle Forms 11g Training
Example Oracle Forms 11g Training
Disclaimer: This demo is built using Oracle Forms 10g R2 (10.1.2.0.2) but it should work with older (as
well as newer) versions of Oracle Forms. This demo also uses the new sample HR schema. If you don’t
have access to this schema, you will have to modify the code to suit your needs.
There are two ways you can use this demo form. First, you can just dig into the sample form provided and
figure out what I did or second, you can follow the steps listed to recreate the sample form.
Note: Because the Layout Wizard added most everything to your canvas, the FRAME will have the Update
Layout property set to Automatically. The quickest way to rearrange your layout is to size the enclosing
Frame to a smaller size and let Forms automatically resize the frame to fit your displayed items and the
scrollbar. At this point, I typically change the Update Layout property to Manually because I don’t always like
the way Forms adjusts the layout. This is a preference issue and not something you need to change.
8. Open the property pallet for the canvas created by the Layout Wizard and set the following properties:
a. Name: EMPLOYEES
b. Window: WINDOW1 (should already be set)
c. Width: 717
d. Height: 260
9. Now it’s time to attach the CRAIG.PLL Forms library. Go to the Object Navigator (ON) and click on the
Attached Libraries node then click on the add button (+) in the ON toolbar. This will open the Attach Library
dialog.
a. Click the Browse… button and navigate to the directory when you stored the CRAIG.pll and select this
library file.
b. Now click the Attach button. Forms Builder will display an alert informing you that the library contains
a non-portable directory specification and will ask if you want to remove the path. Select “Yes” to this Alert.
10. Now we need to create a new window object in the Windows node of the Object Navigator. Open the
property pallet for the new window and set the following properties:
a. Name: SELECTED
b. Title: Selected Rows
c. Width: 313
d. Height: 187
11. Next, we need to create the Canvas that will be used to display the selected records output. Click on
the Canvases node of the Object Navigator and click the add button.
12. Open the property pallet for the new canvas and set the following properties:
a. Name: SELECTED
b. Window: SELECTED
c. Width: 313
d. Height: 187
13. Now, let’s create a CONTROL block to store the Selected? button and the SELECTED_ITEMS Text-Item
(which will be used to display which items were selected).
a. Select the Data Blocks node in the Object Navigator (ON) and then click the add button in the ON
toolbar.
b. The New Data Block alert will display; select the Build a new data block manually option and click the
OK button.
c. Make sure the new block is placed after the EMPLOYEES block in the Data Blocks node.
14. Set the following properties of the control block:
a. Name: CONTROL
b. Database Data Block: No
15. Add a new item (Selected Items) to the Control block and set the following properties:
a. Name: SELECTED_ITEMS
b. Item Type: Text Item
c. Multi-line: Yes
d. Maximum Length: 2000
e. Database Item: No
f. Insert Allowed: No
g. Update Allowed: No
h. Canvas: SELECTED
i. X Position: 12
j. Y Position: 15
k. Width: 288
l. Bevel: Plain
m. Prompt: Selected Rows
n. Prompt Attachment Edge: Top
16. Add another new item (Selected Button) to the Control block and set the following properties:
a. Name: BTN_SELECTED
b. Item Type: Push Button
c. Label: Selected?
d. Number of Items Displayed: 0
e. Canvas: EMPLOYEES
f. X Position: 257
g. Y Position: 232
h. Width: 82
i. Height: 16
At this point, the SELECTED canvas should look similar to the following:
Now it is time to start writing the code to make the form work. Let’s start at the top and work our way
down.
17. Select the Triggers node in Object Navigator. This node is at the top of the object navigator and is
commonly referred to as Form or Module level triggers.
18. Click the Add button and add a WHEN-NEW-FORM-INSTANCE trigger with the following code.
DECLARE
bIgnore BOOLEAN;
BEGIN
-- RG - Name the Record Groups (RG) used to track the
-- rows in the Employees table as well as which records
-- are selected.
Form_Vars.RG1_name := 'EMPLOYEES';
Form_Vars.RG2_name := 'MULTISELECT';
Go_Block('EMPLOYEES');
Execute_Query;
END;
b. Add a second Program Unit Procedure named SET_SELETED with the following code:
vCurrItem := :SYSTEM.CURSOR_ITEM;
<<BLOCK_LIST>>
WHILE ( vCurrItem != vLastItem ) LOOP
vNextNavItem := vName||'.'|| Get_Item_Property(vCurrItem,NEXT_NAVIGATION_ITEM);
vDummy := NULL||Get_Item_Property(vCurrItem,VISIBLE);
22. Now, we need to add the Item trigger code. Open the EMPLOYEES data block and add the following
Block level triggers and code:
a. Trigger: POST-QUERY
DECLARE
bIgnore BOOLEAN := FALSE;
BEGIN
--Add a record to the Record Group.
bIgnore := rec_group.add_value(p_name=>Form_Vars.RG1_name,
p_value=>:Employees.Employee_id,
p_record=>:system.trigger_record);
END;
Note: The Post-Query trigger ensures a record is added to the EMPLOYEES record group; which contains an
entry for every record displayed in the multi-record block.
b. Trigger: WHEN-REMOVE-RECORD
DECLARE
ignore NUMBER;
BEGIN
-- Remove the Current Record from the Record Group (No DML occurs).
ignore := rec_group.delete_value(Form_Vars.RG1_name,:Employees.Employee_id);
END;
Note: The When-Remove-Record trigger ensures when a row is cleared or deleted from the multi-record block
the corresponding record is removed from the EMPLOYEES record group.
23. Now, we need to add the Item level triggers for the EMPLOYEES data block. Open the EMPLOYEES
block and add the following Item Triggers:
a. ROW_NUM – WHEN-NEW-ITEM-INSTANCE
/* This instruction is needed just to prevent the user from navigating to the ROW_NUM column.
I could have set the NAVIGATION property of the item, but this presented problems in the
SET_SELECTED program unit and as this is just a demo form, I didn’t want to spend too
much time on the development. :) */
Next_Item;
b. SELECTED – WHEN-CHECKBOX-CHANGED
DECLARE
bIgnore BOOLEAN;
nIgnore NUMBER;
nRec NUMBER;
BEGIN
IF ( CHECKBOX_CHECKED('EMPLOYEES.SELECTED') ) THEN
--Checked.
bIgnore := Rec_Group.Add_Value(p_name => Form_Vars.RG2_name
,p_value => :EMPLOYEES.EMPLOYEE_ID
,p_inline => FALSE
,p_record => :system.trigger_record);
Set_Selected(TRUE);
ELSE
--Unchecked.
--Check if the value exists first.
nRec := Rec_Group.Delete_Value(p_name => Form_Vars.RG2_name
,p_value => :EMPLOYEES.EMPLOYEE_ID);
Set_Selected(FALSE);
END IF;
END;
Note: This trigger takes care of adding or removing the selected/unselected records from the MULTISELECT
record group.
24. The final Item level trigger is the CONTROL block, BTN_SELECTED When-Button-Pressed trigger. Open
the CONTROL block and add the WHEN-BUTTON-PRESSED trigger and following code to the
BTN_SELECTED item.
DECLARE
nCount1 NUMBER := 0;
nCount2 NUMBER := 0;
nRec NUMBER := 0;
vDummy VARCHAR2(10);
BEGIN
--Get total number of selected records.
nCount1 := Rec_Group.Get_Count(Form_Vars.RG2_name);
nCount2 := Rec_Group.Get_Count(Form_Vars.RG1_name);
25. Lastly, we need to create the Visual Attributes that are used by the SET_SELECTED program unit. Open
the Visual Attributes node and add the following attribute with the listed properties.
a. DEFAULT
i. All properties are left at their default values
ii. In other words, we don’t set any of the values as we want the default values.
b. SELECTED
i. Background Color: r75g100b75
ii. Font Weight: Bold
That’s it! Just compile and run the form. The run product should look similar to the following screen shots.
Description of File Attachments
1. CRAIG.pll - this is Forms PL/SQL Library that contains the Rec_Group pacakge used by this demo.
2. How to_Select Multiple Records in a Block using an RG.doc - This demo in Microsoft Word 2003 format.