Example: custlist.4gl (Function select_customer)

The select_customer function opens a window with a list of customers, and lets the user select a customer from this list.

The select_customer function:
  1 PUBLIC FUNCTION select_customer() RETURNS (INTEGER,STRING)
  2 
  3   DEFINE sql_cond STRING
  4   DEFINE ret_num LIKE customer.cust_num
  5   DEFINE ret_name LIKE customer.cust_name
  6   DEFINE x INTEGER
  7   DEFINE cnt INTEGER
  8 
  9   LET ret_num = -1
 10 
 11   OPEN WINDOW w_cust WITH FORM "custlist"
 12 
 13   DIALOG ATTRIBUTES(UNBUFFERED)
 14 
 15      CONSTRUCT BY NAME sql_cond ON customer.cust_name
 16      END CONSTRUCT
 17 
 18      DISPLAY ARRAY custarr TO sa_cust.*
 19      END DISPLAY
 20 
 21      BEFORE DIALOG
 22         LET cnt = custarr_fill("1 = 1")
 23 
 24      ON ACTION fetch ATTRIBUTES(ACCELERATOR="F5")
 25         LET cnt = custarr_fill(sql_cond)
 26         IF cnt == 0 THEN
 27             NEXT FIELD cust_name
 28         ELSE
 29             NEXT FIELD s_num
 30         END IF
 31 
 32      ON ACTION accept
 33         LET x = DIALOG.getCurrentRow("sa_cust")
 34         IF x > 0 THEN
 35            LET ret_num = custarr[x].cust_num
 36            LET ret_name = custarr[x].cust_name
 37            EXIT DIALOG
 38         END IF
 39 
 40      ON ACTION cancel
 41         EXIT DIALOG
 42 
 43   END DIALOG
 44 
 45   CLOSE WINDOW w_cust
 46 
 47   RETURN ret_num, ret_name
 48 
 49 END FUNCTION
Note:
  • Line 1 declares the select_customer function, with no parameters. The function returns an integer and a string: this is the number and name of the customer selected by the user.
  • Lines 3 thru 7 declare local function variables.
  • Line 9 initializes the ret_num variable to -1: If the user cancels the list dialog, the code at lines 35 and 36 will not be executed and the function will return -1 and NULL, 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 11 opens a new window with the custlist form.
  • Lines 13 thru 43 implement the DIALOG multiple-dialog with a CONTRUCT and DISPLAY ARRAY sub-dialogs, to let the user filter rows from the customer table, and choose a customer from the list.
  • Lines 15 thru 16 implement the CONSTRUCT sub-dialog, that fills the sql_cond string variable with the SQL condition corresponding to the criteria entered in the cust_name field.
  • Lines 18 thru 19 implement the DISPLAY ARRAY sub-dialog, to control the TABLE container of the form showing the customer list, from the data rows of the custarr dynamic array.
  • 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.
  • Lines 24 thru 30 declare an action handler for the "fetch" action, that takes the SQL condition produced by the CONSTRUCT, to re-fill the program array by calling the custarr_fill function. Action can be fired by a click on the form button bound to the action, or by the F5 key. If no rows where found, we put the focus in the query field with NEXT FIELD cust_name. Otherwise, when rows are found, we put the focus into the TABLE container with a NEXT FIELD s_num instruction.
  • Lines 32 thru 38 implement the action handler to validate the dialog, for the OK button. If there is a current row, we set the ret_num and ret_name variables with the number and name of the current customer row.
  • Lines 40 thru 41 define the action handler to cancel the dialog, for the Cancel button.
  • Line 38 closes the window used by the function.
  • Line 47 returns the value hold in the ret_num and ret_name variables. A positive number identifies the customer number selected by the user, while -1 means that the choice was canceled.