Lab for Monday, March 2, 1998

 

 

How to Implement a Drop Down List on A Canvas

 

 

I've mentioned several times in class that you can implement a drop down list in a block without using a list of values (LOV). In this lab you'll implement a simple drop down list in your boat block. Before you can do so, you need to prepare a few things:

 

  1. Create a code table that has two columns: code_id (number) and description (varchar2(15)). Code_ID is the primary key field for the code table. Populate the code table with some values. For example, you might want to insert the following rows depicting the make of a boat:
  2.  

    1, Tartan

    2, Catalina

    3, C&C

    4, Hunter

    5, Swan

    6, Dehler

    7, Hylas

    8, Fontaine Pajot

    9, Beneteau

    10, Jenneau

     

  3. On your boat canvas hide the make field by pulling up the property sheet for the make item and setting the visible attribute to false. Note that your make item should be a number, not a varchar2 (if your make column is a varchar2, either add another field to your boat table and make a code table for something like rigging, or alter your table to make the data type of the make column a number).
  4.  

  5. Pull up the canvas layout editor and add a new item to it, naming it "make_list" or something similar. The item should also be numeric, not a base table item, and the item type should be list item. The list style should be poplist. Also, set the "synchronize with item" field to make. Basically, this ensures that whatever value you choose on the list is automatically put in the make column. All of these attributes are set using the property sheet.
  6.  

  7. Create an alert called Database_Error that only has an OK button. The text of the alert should tell the user that a pull down list could not be populated due to a database error.
  8.  

  9. Create a program unit called Init_Codes_Small and type in the following code:
  10.  

     

    -----------------------------------------------------

    -- Function Init_Codes_Small

    -- Written by: Andreas M. Olligschlaeger

    -- Date Written: October 1996

    -- Last Modified:

    -- Purpose: Populates a list items from a code table

    -- with single code fields. Returns 0 on

    -- success, -1 on failure.

    -----------------------------------------------------

     

    FUNCTION INIT_CODES_SMALL(V_TABLE IN VARCHAR2,V_KEY IN VARCHAR2, v_item IN VARCHAR2) RETURN INTEGER IS

    group_id RecordGroup;

    list_id Item;

    v_test number;

    BEGIN

    group_id := Create_Group_From_Query('vector',

    'select DESCRIPTION,TO_CHAR('||V_KEY||') from '||V_TABLE||

    ' ORDER BY DESCRIPTION');

    IF id_null(group_id) THEN

    v_test := SHOW_ALERT('DATABASE_ERROR');

    RETURN -1;

    end if;

    v_test:=Populate_Group('vector');

    list_id := Find_Item(v_item);

    IF id_null(list_id) THEN

    v_test := SHOW_ALERT('DATABASE_ERROR');

    RETURN -1;

    end if;

    Populate_List(list_id,group_id);

    Delete_Group(group_id);

    RETURN 0;

    END;

     

  11. Add a line to your when-new-form-instance trigger that calls the Init_Codes_Small function. For example, if you named your code table Make_Codes, the name of your boat block is B_Boat, and the list item in the boat block is named Make_List, then call the function using the following code:

 

Declare

V_test number;

 

Begin

V_test := Init_codes_small('Make_Codes','Code_ID','B_Boat.Make_List');

End;

 

Your pull down list will now automatically be populated each time you fire up the form. Also, when a user selects a make using the pull down menu, the correct code id will automatically be inserted into the boat table. A source code listing for a function that populates a list item for code table using multiple codes can be accessed from the web page.

 

Back to Main Page