Practical 6 B

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Creating Custom Forms

Lesson A
Creating Custom Forms
Lesson B

Lesson Objectives
• Learn about default system messages and how to suppress them
• Create alerts and messages to provide system feedback
• Create applications that avoid user errors
• Learn how to trap common runtime errors

Controlling System Messages


System messages are assigned levels by Oracle depending on their severity. Levels range from 0 to 25 and
above where 0 indicates low severity and 25 indicates high. Messages can be suppressed by setting the variable
:SYSTEM.MESSAGE_LEVEL. All messages with severity below the level in the variable will not be
displayed to the user. The variable is typically set using a trigger on the PRE-FORM event.

H.Seegobin 1
Creating Custom Forms
Lesson A
Q1: To Create a PRE-FORM Trigger:
1. From Object navigator, Right-Click the Trigger node (which is just below the
Receiving_Form), select SmartTriggers and then PRE-FORM, as shown below. The
PL/SQL Editor opens and type the following commands:
:SYSTEM.MESSAGE_LEVEL := 25;
2. Compile the trigger. Save and Run the Form.
3. Select a Shipment value and update the record.
4. What did you noticed in the message line?
5. Did you see any system message?

H.Seegobin 2
Creating Custom Forms
Lesson A
Providing System Feedback
It is a good idea for the form developer to display information to the user about how the program is working.
For example, when the user saves changes to the database, a message should be displayed indicating that the
save was successful. There are two ways to display information to the user: using custom messages and alerts.
A custom message is a short (200 characters or less) message displayed in the message line of the form. An
alert is a dialog box that displays a message and presents the user with one or more buttons to click to dismiss
the dialog. The buttons either indicate a user confirmation of the message or a choice between two or more
options (such as Save or Cancel) that the user must make. After the alert button has been pressed, ELSIF logic
can be used to determine what to do next:

Q2: To Add Custom Messages to the Receiving_Form


1. Open the Layout Editor, right-click the Update button and then click PL/SQL Editor
to open the button trigger.
2. To display a custom message when the user updates an existing record, add the
following command as the last command in the trigger:
MESSAGE(‘Record Updated’);
3. Compile the trigger and Save and Run the Form.
4. Test the trigger by updating a record in the shipment_line.
5. What happen when you click on the Update Button?
Note: You can create similar custom message when inserting, deleting and saving a record.

H.Seegobin 3
Creating Custom Forms
Lesson A
Alerts
Q3: To Create the alert:
1. Select the Alert node from the Object Navigator window and click Create button.
2. Double Click the Alert icon beside the new alert to open its Property Palette and then
change its properties as follows:

To Display an alert in a form, you use the SHOW-ALERT built-in function. In programming , a function always
returns a value. The SHOW-ALERT function returns a numeric value that corresponds to the button that the
user clicks on the alert.

H.Seegobin 4
Creating Custom Forms
Lesson A
Q4: To Create the Program Units to display the alert:
1. In the object Navigator window, select the Program Units node and then click the
Create button to create a new program unit.
2. Type DISPLAY_ALERT for the new program unit name, make sure the procedure
option button is selected and then click OK.
3. Modify the procedure so it appears as below:
PROCEDURE DISPLAY_ALERT IS
alert_button NUMBER;
BEGIN
alert_button := SHOW_ALERT('UPDATE_ALERT');
IF alert_button = ALERT_BUTTON1 THEN
COMMIT;
MESSAGE('Record Updated');
ELSIF alert_button = ALERT_BUTTON2 THEN
ROLLBACK;
MESSAGE('Record not Updated');
END IF;
END;
4. Open the Layout Editor, right-click the Update button then click PL/SQL Editor
5. Delete the Commit; command and replace it with the following command that calls
the DISPLAY_ALERT program Unit:
6. DISPLAY_ALERT;
7. Compile the trigger, correct any syntax errors, close the PL/SQL editor and save the
form.
8. Run and test the application;’

H.Seegobin 5
Creating Custom Forms
Lesson A
Avoiding User Errors
It is good programming practice to prevent users from entering invalid data or clicking buttons at inappropriate
times (such as clicking a save button when a form is not fully filled out). Forms Builder allows the form
developer to specify the data type of form fields as well as other properties to validate entered data such as
maximum and minimum values. If a field is tied to an LOV, it can be validated against values in the LOV by
setting the Validate From List Property. If other validation of data is needed besides that supported by Forms
Builder, custom validation may be coded in a validation trigger. The trigger is tied to the WHEN-VALIDATE-
ITEM event and raises a FORM-TRIGGER-FAILURE. Command button on forms should be disabled at times
when they should not be pressed. This is done using the SET-ITEM-PROPERTY built-in function.

The developer should disable fields that the user should not edit (such as primary key fields). Unfortunately
there is not a straightforward way to disable a text item. Instead, it is accomplished indirectly in two ways.
First, the field should be made nonnavigable (user cannot use tab key to get to the field) by setting the Keyboard
Navigable property to No. Second, the developer must disable mouse clicks in the field by writing a trigger to
move the form focus to another form item.

H.Seegobin 6
Creating Custom Forms
Lesson A
Validating Form Input Values
Q5: Text Item Validation properties:
1. Open the Student Form as shown below and name it 6BStudent.

2. Create a Sequence called it S_ID_Sequence and make the starting value to be 10


3. Open the Form in Layout Editor, select State text item, press and hold the Shift key,
and then click the Class text item to select the text items as an object group. Right-
click to open the property palette of the selected items.
4. Change the data type to Alpha and make the Maximum Length property to 2.
5. Open the Property Palette for the Faculty ID text Item, scroll down to the List of
Values property node, change the Validate From list Property to Yes, close the
property palette then save the form.
6. Run the Form and click Create button. A new value appears in the Student ID Text
Item
7. Enter 22 for the State Text Item. What did you noticed?
8. Enter WI for the State Text item. What did you noticed?
9. Type 10 for the Faculty ID text Item. What did you noticed?
10. Now click on the SAVE button. What did you noticed?

H.Seegobin 7
Creating Custom Forms
Lesson A
Form Validation Triggers
Q6: To Create and test an item validation trigger:
1. In the Layout Editor, select the Class text item, right-click point to SmartTriggers
and then click WHEN-VALIDATE-ITEM
2. Type the following command in the PL/SQL Editor to create the validation trigger:
IF NOT :STUDENT.S_CLASS IN
(‘FR’, ‘SO’, ‘JR’, ‘SR’) THEN
MESSAGE(‘Legal Values are FR, SO, JR, SR’ );
RAISE FORM_TRIGGER_FAILURE;
END IF;
3. Compile the trigger and save the form.
4. Run the form and Click Create button, enter appropriate values for the text field. Enter
AA for the Class and press the tab. What did you noticed?

Disabling Form Command Buttons to Avoid User Errors


Q7: To change and test the button properties:
General syntax:
SET_ITEM_PROPERTY(‘ITEM_NAME’, PROPERTY_NAME,
PROPERTY_VALUE);
1. From the Layout Editor, open the Property Palette for the Save New button and
change the Enable Property to NO. Then close the property Palette.
2. Open the PL/SQL Editor for the Create Button and add the following commands as
the last line in the trigger:
SET_ITEM_PROPERTY(‘SAVE_BUTTON’, ENABLED, PROPERTY_TRUE);
3. Compile the trigger and save the form.
4. Run the form. What happens to the SAVE button when the form is loaded?
5. Click Create. What happens to the SAVE button when the Create button is clicked?
Disabling Text Item Navigation
Q8: To make the text item nonnavigable and switch the form focus when
the user clicks the mouse in the text item:
1. Open the property palette for the S_ID text item. Change the Keyboard Navigable
property to NO and then close the Property Palette.
2. Open the PL/SQL Editor for the S_ID text item and select the WHEN-MOUSE-UP
event from the list.
3. type the following command in the Editor:
GO_ITEM(‘STUDENT.CREATE_BUTTON’);
4. Save and run the form. Where is the insertion point when the form is loaded?
5. Click on the Student ID text item. What did you noticed?
6. Press the Tab 3 times. What did you noticed?

H.Seegobin 8
Creating Custom Forms
Lesson A

Trapping Common Runtime Errors


Q9:Generating Runtime Errors:
1. Open the ItemInventory.fmb form and save it as 6BItemInventory.
2. Run the form. Make sure the insertion point is in the Item ID field, click open the
LOV display, select Item ID 1 and then click OK. The item values appear in the form
text items. You could edit the item data and click the Update button in the Items
frame to edit the record. You could also delete the data item if you wish.
3. Place the insertion point in the Inventory ID field, then open the LOV display. Select
Inventory ID 25 and then click OK.
4. Change the value of the price to 219.95 and then click Update within the Inventory
Items frame.
5. Click Clear in both of the frame to refresh the form.
Now you deliberately generate errors while updating and deleting records and view error
messages.
1. Place the insertion point in Item ID and open the Item ID 2.
2. Click Delete in the item frame. What did you noticed?
3. Place the Insertion point in the Inventory ID field and open the LOV and select ID 1
and click OK.
4. Delete the current QOH field value and type letter a as the new QOH value.
5. Click Update button. What did you noticed?

A form trigger can be used to trap common errors and display a more meaningful error message instead of the
standard one. The trigger corresponds to the ON-ERROR event for the entire form. The general syntax for an
on error trigger is shown in Figure 6-27:

The ERROR_CODE and DBMS_ERROR_CODE built-in procedures contain the most recent FRM- and ORA-
error codes respectively.

H.Seegobin 9
Creating Custom Forms
Lesson A
Q10 : To create and test the ON-ERROR trigger :

1. In the Forms Builder, make sure that the Object Navigator window is open. To
create a form-level trigger, select the trigger node directly below the form module
then click the create button.
2. Select the ON-ERROR event from the list and then click OK.
3. Type the following commands:
--trap FRM errors
IF ERROR_CODE = 50016 THEN
MESSAGE('Please enter a value that is a number');
ELSIF ERROR_CODE = 40735 THEN
-- TRAP ORA ERRORS
IF DBMS_ERROR_CODE = -2292 THEN
MESSAGE('This record is referenced by other database objects');
ELSE
--general error handler
MESSAGE('DBMS_ERROR_TEXT');
END IF;
ELSE
-- general error handler
MESSAGE('ERROR_TEXT');
END IF;
4. Save the form and run it.
5. Try deleting or updating a record with abnormal values. What did you noticed?

H.Seegobin 10

You might also like