Progress
Programming
Handbook


Creating Browse Rows

In an updatable browser, you might want to allow the user to add new records to the database. Programmatically, this requires three separate steps:

  1. Create a blank line in the browse viewport with the INSERT–ROW() method and populate it with new data.
  2. NOTE: You can use the INSERT–ROW() browse method in an empty browser. It places a new row at the top of the viewport.

  3. Use the CREATE statement and ASSIGN statement to update the database.
  4. Add a reference to the results list with the CREATE–RESULT–LIST–ENTRY( ) method. (This step is only necessary if you do not plan to reopen the query after the update. However, this method makes reopening the query unnecessary for most applications.)

All three steps are required to create the record and keep the database, query, and browse in sync. Also, there are several possible side effects to allowing the user to add a record through an updatable browse. They include placing new records out of order and adding records that do not match the query. To eliminate these side effects, you can reopen the query after each new record.

The code example below shows one possible algorithm for creating new records with an updatable browse:

p-br09.p
DEFINE QUERY q1 FOR item SCROLLING. 
DEFINE BROWSE b1 QUERY q1 DISPLAY item-num item-name price 
    ENABLE item-name price WITH 10 DOWN SEPARATORS  
    TITLE "Update Inventory".  
DEFINE VARIABLE method-status AS LOGICAL.  
DEFINE VARIABLE temp-rowid AS ROWID. 
DEFINE BUTTON b-new LABEL "Add New Record" SIZE 45 BY 1.5.  
DEFINE FRAME f1 
    b1 skip 
    b-new 
        WITH SIDE-LABELS ROW 2 CENTERED NO-BOX. 
ON CHOOSE OF b-new DO: 
    method-status = b1:INSERT-ROW("AFTER"). 
END.  
ON ROW-LEAVE OF BROWSE b1 DO: 
    IF b1:NEW-ROW IN FRAME f1 THEN DO: 
        DO ON ERROR UNDO, RETURN NO-APPLY: 
            CREATE item. 
            ASSIGN item-num = NEXT-VALUE(next-item-num) 
                   INPUT BROWSE b1 item-name price. 
            method-status = b1:CREATE-RESULT-LIST-ENTRY(). 
        END.  
    END. 
END. 
OPEN QUERY q1 FOR EACH item NO-LOCK. 
ENABLE ALL WITH FRAME f1. 
WAIT-FOR WINDOW-CLOSE OF CURRENT-WINDOW. 

In this example, the user requests a new record by choosing a button. The button triggers the INSERT–ROW( ) browse method. This method takes two possible strings: BEFORE and AFTER, indicating where to place the new record—before or after the current selected row. The method places a new blank browse row in the browse viewport. The method has no effect on the database or the results list of the query.

Since you know that you have allowed the user to add new rows, you have to check for them. A ROW–LEAVE trigger provides the perfect vehicle for checking for this special case. Because the code does not contain the NO–ASSIGN option of the DEFINE BROWSE statement, the normal default row assignment occurs for updated existing records.

The NEW–ROW attribute tells you whether the current row is a new one. When you encounter one, you have to create a new blank database record and populate it with all the necessary data, which may come exclusively from the browse or from a mixture of sources. In this example, the item-num is generated by an existing database sequence.

At this point, the browse has correct data, the database has correct data, but the query results list does not. The CREATE–RESULT–LIST–ENTRY() method takes care of this task.

The following screen shows a new record being entered into the browse:


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095