Example: stocklist.4gl

The stocklist.4gl module defines a 'zoom' module, to let the user select a stock item from a list.

The stocklist.4gl module:
  1 SCHEMA custdemo
  2 
  3 PRIVATE TYPE t_stock RECORD
  4            stock_num LIKE stock.stock_num,
  5            description LIKE stock.description
  6         END RECORD
  7 
  8 PUBLIC FUNCTION select_stock_item() RETURNS INTEGER
  9   DEFINE stockarr DYNAMIC ARRAY OF t_stock,
 10          rec t_stock,
 11          ret_num LIKE stock.stock_num,
 12          x INTEGER
 13 
 14   LET ret_num = -1
 15 
 16   DECLARE c_stock CURSOR FOR
 17     SELECT stock_num, description
 18       FROM stock
 19      ORDER BY stock_num
 20 
 21   FOREACH c_stock INTO rec.*
 22     LET x = x + 1
 23     LET stockarr[x] = rec
 24   END FOREACH
 25 
 26   OPEN WINDOW w_stock WITH FORM "stocklist"
 27 
 28   LET int_flag = FALSE
 29   DISPLAY ARRAY stockarr TO sa_stock.*
 30      BEFORE DISPLAY
 31         CALL DIALOG.setActionActive("accept",(stockarr.getLength()>0))
 32      AFTER DISPLAY
 33         IF NOT int_flag THEN
 34            LET ret_num = stockarr[arr_curr()].stock_num
 35         END IF
 36   END DISPLAY
 37 
 38   CLOSE WINDOW w_stock
 39 
 40   RETURN ret_num
 41 
 42 END FUNCTION
Note:
  • Lines 1 declares the database schema for LIKE instructions.
  • Lines 3 thru 6 define the t_stock type with the stock number and description fields, using the same data type than the stock_num and descrition columns of the stock table.
  • Line 8 declares the select_stock_item function, with no parameters. The function returns an integer: this is the stock number selected by the user.
  • Lines 9 thru 12 declare local function variables.
  • Line 14 initializes the ret_num variable to -1: If the user cancels the list dialog, the code at line 34 will not be executed and the function will return -1, indicating that the user has canceled the choice.
  • Lines 16 thru 24 use an SQL cursor to fetch rows from the stock SQL table into the stockarr program variable.
  • Line 26 opens a new window with the stocklist form.
  • Lines 28 thru 36 implement the DISPLAY ARRAY dialog to let the user choose a stock item from the list.
  • Lines 34 thru 35 check that the user has validated the row selection, and sets the ret_num variable with the number of the selected stock item. This is the value that will be returned by the function.
  • Line 38 closes the window used by the function.
  • Line 40 returns the value hold in the ret_num variable. A positive value identifies the stock item number selected by the user, while -1 means that the choice was canceled.