Example: custlist.4gl (MAIN block)

The MAIN block implements a WHILE loop, to let the user first enter a query filter, then browse rows in the filtered result set, and repeat the same process until the Cancel button is selected.

The MAIN block in custlist.4gl:
  1 MAIN 
  2   DEFINE sql_cond STRING,
  3          cust_num LIKE customer.cust_num
  4          
  5   DEFER INTERRUPT
  6   
  7   CONNECT TO "custdemo"
  8   
  9   OPEN FORM fca FROM "custlist"
 10   DISPLAY FORM fca
 11   
 12   WHILE TRUE
 13   
 14     LET sql_cond = custlist_query()
 15     IF sql_cond IS NULL THEN
 16        EXIT WHILE
 17     END IF
 18   
 19     LET cust_num = custarr_display(sql_cond)
 20     CASE
 21     WHEN cust_num < 0
 22        EXIT WHILE 
 23     WHEN cust_num == 0
 24        MESSAGE "Now rows found!"
 25     OTHERWISE
 26        MESSAGE SFMT("Customer #%1 was selected",cust_num)
 27     END CASE
 28 
 29   END WHILE
 30 
 31 END MAIN
Note:
  • Lines 12 thru 29 define the WHILE loop.
  • Lines 14 to 17 do the query filter input by calling the custlist_query function.
  • Line 15 tests the value returned by the query function. A NULL indicates that the user has canceled the CONSTRUCT instruction. In this case, we just leave the WHILE loop with an EXIT WHILE instruction, and the program stops.
  • Lines 19 thru 27: We call the custarr_display function, which handles the customer record list. This function returns the number of the customer selected by the user.
  • Line 21: The custarr_display function returns -1, if the user has canceled the DISPLAY ARRAY dialog. In this case we leave the WHILE loop and the program stops.
  • Line 23: Zero is returned as customer number, if no rows where found by the SQL query, and a message is displayed to the user.
  • Line 25: A positive number indicates that a customer record has been choosen by the user.