Previous | Table of Contents | Next

Page 487

Week 3

Day 18

Developing an
Application with Oracle
Power Objects

In the previous lesson, you became acquainted with Oracle Power Objects. You learned how to define a database session, how to create an application object, and how to create a simple form and a master-detail form. In this lesson, you continue exploring some of the features of Power Objects including the following:

Page 488

Methods and Method Code

NEW TERM
A method is a set of instructions that are executed by an object in response to an event or a call to the object. Each type of object has a set of predefined methods called standard methods. The set of instructions—which are Oracle Basic statements—executed for a method is known as method code. Some methods may have default processing that is executed if a developer has not written any method code.

The methods for an object are listed on the object's property sheet below the properties. On the property sheet, a small diamond is displayed to the left of each property, and a small arrowhead is displayed to the left of each method. Let's look at an example of a method for a form.

  1. To begin, create another form and name it MyForm.
  2. Add two text fields to the form: XPos and YPos.
  3. Bring up the property sheet for the form and scroll down to the MouseOver method. The MouseOver method is triggered when the mouse moves over the object.
  4. As shown in Figure 18.1, click on the MouseOver method and enter:
     XPos.value = x
     YPos.value = y

Figure 18.1.
Method code for
MouseOver.

Page 489

This script will assign the current x and y positions to the XPos and YPos fields, respectively. Click on the MouseOver method title to close the window. Alternatively, you can invoke the Code Editor window by clicking the Code Editor button at the top of the property sheet (it resembles a yellow pencil on a page). As you can see in Figure 18.2, the Code Editor window can be resized, making it convenient for entering a large block of method code.

Figure 18.2.
Using the Code Editor
window to specify the
method code for
MouseOver.

Close the property sheet and test the form by right-clicking Run Form. Figure 18.3 illustrates what your form should display in Run-Time mode.

Figure 18.3.
Testing the method
code for MouseOver.

Page 490

Validating User Input

Let's look at a commonly used method for a control: the Validate method. This method is used to enforce the business rules for an object. For example, suppose that one of the business rules for Flugle College is that the student ID must begin with a 1. To specify this business rule, select the Student_ID text field on the Student form, and bring up its property sheet. Scroll down to the Validate method—it's the last method for the object. Enter the Oracle Basic statements shown in Listing 18.1.

Listing 18.1. Enforcing a business rule in the method code for Validate.

IF LEFT(newval,1) = "1" THEN
    Validate = TRUE
ELSE
    Validate = FALSE
    MSGBOX("Student ID must begin with a 1"
END IF

The business rule is expressed in the Oracle Basic statement, IF-THEN-ELSE. The first line uses the LEFT function to return the first character of the Student_ID being validated. If the first character is equal to 1, the Validate function will return TRUE, indicating that the new value passes validation; otherwise, the Validate function will return FALSE, preventing the user from navigating to another control or committing the change. Also, the MSGBOX function is used to display an error message to the user.

Let's test the method code.

  1. Close the property sheet for Student_ID.
  2. Click the Run Form button on the toolbar to test the form.
  3. In the first record, try setting the first character of Student_ID to 2.
  4. Try clicking another field. As you can see in Figure 18.4, the method code displays an error message, as expected.
  5. To exit the form, click on the Rollback button on the toolbar (it resembles a purple arrow) and then the Stop button.

Page 491

Figure 18.4.
Testing the method
code for Validate.

Displaying Calculated Values in a Form

In the previous lesson, you learned how to display a lookup field that used the built-in function SQLLOOKUP. Now, you'll see how you can construct a field which will display a value that is calculated from the value of other controls in the form.

As an example, let's construct a form that displays information about each course.

  1. Set the following properties for the form:
            RecSrcSession    Flugle
    
            RecordSource     Course
    
            OrderBy          Department_ID, Course_ID
    
            Label            Course Information
    
            Name             CourseInformation
    
    1. Also, add text fields and static text to the form, as shown in Figure 18.5.
    2. The calculated field is named Total_Cost; it will reflect the cost of a course, which is based on the cost per unit, $350, plus any additional fees. Select the calculated field, named Total_Cost, and right-click Property Sheet. As shown in Figure 18.6, set these properties:
         DataSource     = Units.value*350 + Additional_Fees.value
    
         Datatype       Long Integer
    
         Name           Total_Cost
    

    Page 492

    Figure 18.5.
    The CourseInforma-
    tion form.

    Figure 18.6.
    Setting the
    DataSource property
    for the calculated
    field.

    1. Exit the property sheet; it's time to test the form and see if the calculated field is working.
    2. Click on the Run Form button on the toolbar. As you can see in Figure 18.7, the calculated field is displaying the correct amount for each record.

    Previous | Table of Contents | Next

    Используются технологии uCoz