Progress
Language Tutorial
for Character


The UPDATE Technique

A separate mode for updating data protects data from unintended changes. The technique shown in this section relies on a statement normally reserved for application-driven programs and a special type of frame. To understand the technique, you need to learn about the UPDATE statement and the dialog box.

UPDATE Statement

To put it succinctly, the UPDATE statement is a powerful statement that does the work of the DISPLAY, ENABLE, ASSIGN, and WAIT-FOR statements. The procedure below is a complete application that does much of the work of the last programming example:

FIND FIRST Item. 
REPEAT: 
  UPDATE Item WITH 2 COLUMNS.
  FIND NEXT Item. 
END. 

Here’s what this application does:

  1. Finds the first Item record.
  2. The UPDATE statement:
    • Creates a default display.
    • Displays data from the record.
    • Enables the widgets.
    • Blocks execution to allow the user to interact with the widgets.
    • Waits for the GO event function.
    • Assigns changes after receiving the GO event function. (Pressing F2 executes GO. After finishing with each record, the user presses F2 to move on to the next record.)
  3. Finds the next record.
  4. Iterates until it encounters an error condition: either no more records exist or the user issues the ENDKEY (F5) event function. If the user issues ENDKEY, any changes made to the current record are discarded.

This application is not very flexible, but illustrates both the power of UPDATE and why UPDATE should normally not be used with an event-driven procedure. As you saw in the description above, UPDATE blocks application while waiting for GO. If you mixed an update statement with a WAIT-FOR statement, you would have explicit application blocking and implicit application blocking conflicting with each other. The result would be confusing to the end user. UPDATE should be used by itself. As you you’ll see later in the section, you can combine UPDATE with a dialog box to provide a separate update mode for database records.

Figure 8–12 shows the data movement with UPDATE.

Figure 8–12: Data Movement with the UPDATE Statement

This is a partial syntax for the UPDATE statement.

SYNTAX
UPDATE 
  {   field 
    | SPACE [ ( n ) ]
    | SKIP [ ( n ) ]
  } ... { [ frame-phrase ] } 

Dialog Boxes

A dialog box is a container widget that disables all widgets located in other container widgets while the dialog box is enabled. Since all other widgets are disabled, you can execute an UPDATE statement with a dialog box from within a block of code that includes a WAIT-FOR statement. Since the implied application blocking of the UPDATE statement cannot interfere with the explicit application blocking of the WAIT-FOR, this combination elegantly creates an update mode.

You create a dialog box by specifying the VIEW-AS DIALOG BOX option in the frame phrase.

The dialog box appears on top of all other frames and stays there until the user or the application dismisses it. In this case, completing the UPDATE statement dismisses the dialog box. So, if the user presses F2 (GO), UPDATE assigns the changes and the dialog box disappears. If the user presses F4 (ENDKEY), the changes are discarded and the dialog box disappears.

The use of the GO and ENDKEY keystrokes are not very elegant in an event-driven application. Fortunately, the DEFINE BUTTON statement supports options that let buttons automatically execute the GO and ENDKEY event functions: AUTO-GO and AUTO-ENDKEY. If you define a button labeled OK with the AUTO-GO option and another button labeled Cancel with the AUTO-ENDKEY option, you have an interface that fits in nicely with the conventions of an event-driven application.

UPDATE and Dialog Box Programming Example

Follow these steps for a demonstration:

  1. Open lt-08-04.p and run it. The display appears as shown below, with the fields on the form represented as text widgets:
  2. Choose Update. The update dialog box appears:
  3. Note that you cannot move focus to any widget outside the dialog box.

  4. Change the data and choose Cancel. The dialog box disappears and the main form remains unchanged.
  5. Choose Update again, change some data, and Choose OK. The dialog box disappears and the main form shows your changes.
  6. Choose Exit, then press SPACEBAR to return to the Procedure Editor.

This is the code for this application:

lt-08-04.p
       /**********  DEFINE QUERY  **********/
       DEFINE QUERY Item-Query FOR Item.

       /**********  DEFINE VARIABLES  **********/
       DEFINE VARIABLE Current-Record AS ROWID. 
       /**********  DEFINE FORM  **********/
       {lt-08-f1.i}

      /**********  DEFINE TRIGGERS  **********/
       {lt-08-t1.i}   

       ON CHOOSE OF btn-Update 
        DO:
/*2*/      Current-Record = ROWID(Item).
          FIND FIRST Item WHERE ROWID(Item) = Current-Record
             EXCLUSIVE-LOCK.
          IF AVAILABLE(Item) THEN DO:
/*3*/          UPDATE Item.Item-Name Price On-Hand Allocated Re-Order 
              On-Order Cat-Page Cat-Description btn-OK btn-Cancel 
                  WITH FRAME Dialog1.
              DISPLAY Item-Num Item-Name Price On-Hand Allocated Re-Order 
                On-Order Cat-Page Cat-Description WITH FRAME Frame1.
            END.
/*4*/       ELSE DO:
            FIND FIRST Item WHERE ROWID(Item) = Current-Record NO-LOCK.
            MESSAGE "Record in use. Unable to update." 
                VIEW-AS ALERT-BOX WARNING BUTTONS OK Title "Update Error".
            END.
        END.

      /**********  MAIN LOGIC  **********/
/*1*/  OPEN QUERY Item-Query FOR EACH Item NO-LOCK.
      GET FIRST Item-Query.
     DISPLAY Item.Item-Num Item-Name Price On-Hand Allocated Re-Order
        On-Order Cat-Page Cat-Description WITH FRAME Frame1 USE-TEXT.
     ENABLE btn-Prev btn-Next btn-Update btn-Exit WITH FRAME Frame1.
      WAIT-FOR CHOOSE OF btn-Exit.
      CLOSE QUERY Item-Query. 

These notes help to explain the code:

  1. In the main code block, the NO-LOCK option lets all users have complete access.
  2. The procedure saves the ROWID, as before.
  3. If the upgrade to EXCLUSIVE-LOCK is successful, then the UPDATE statement and dialog box combination take over.
  4. Again, if the lock upgrade fails, refind the record with NO-LOCK and notify the user.

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