Example: custlistmod.4gl (Function custarr_display)

The custlistmod.4gl module implements the DISPLAY ARRAY dialog in the custarr_display function.

The custarr_display function in custlistmod.4gl:
  1 PRIVATE FUNCTION custarr_display_modif() RETURNS ()
  2   DEFINE x INTEGER
  3   DEFINE rec t_custrec
  4 
  5   LET int_flag = FALSE
  6 
  7   DISPLAY ARRAY custarr TO sa_cust.*
  8           ATTRIBUTES(UNBUFFERED,
  9                      CANCEL = FALSE,
 10                      DOUBLECLICK = update)
 11 
 12      ON APPEND
 13         LET x = DIALOG.getCurrentRow("sa_cust")
 14         INITIALIZE rec.* TO NULL
 15         LET rec.cust_num = 1000 + x
 16         LET rec.cust_name = "<undefined>"
 17         INPUT rec.* WITHOUT DEFAULTS FROM sa_cust[scr_line()].* ;
 18         IF NOT int_flag THEN
 19             TRY
 20                 INSERT INTO customer VALUES (rec.*)
 21                 LET custarr[x] = rec
 22             CATCH
 23                 ERROR SQLERRMESSAGE
 24                 LET int_flag = TRUE
 25             END TRY
 26         END IF
 27 
 28      ON UPDATE
 29         LET x = DIALOG.getCurrentRow("sa_cust")
 30         LET rec = custarr[x]
 31         INPUT rec.* WITHOUT DEFAULTS FROM sa_cust[scr_line()].* ;
 32         IF NOT int_flag THEN
 33             TRY
 34                 UPDATE customer SET customer.* = rec.*
 35                        WHERE cust_num = rec.cust_num
 36                 IF sqlca.sqlerrd[3] == 0 THEN
 37                    ERROR "The row has been deleted by another user."
 38                    LET int_flag = TRUE
 39                 ELSE
 40                    LET custarr[x] = rec
 41                 END IF
 42             CATCH
 43                 ERROR SQLERRMESSAGE
 44                 LET int_flag = TRUE
 45             END TRY
 46         END IF
 47 
 48      ON DELETE
 49         LET x = DIALOG.getCurrentRow("sa_cust")
 50         LET int_flag = NOT comutils.mbox_yn("Customers",
 51                               "Do you want to delete the current row?")
 52         IF NOT int_flag THEN
 53             TRY
 54                 DELETE FROM customer WHERE cust_num = custarr[x].cust_num
 55             CATCH
 56                 ERROR SQLERRMESSAGE
 57                 LET int_flag = TRUE
 58             END TRY
 59         END IF
 60 
 61   END DISPLAY
 62 
 63 END FUNCTION
Note:
  • Line 1 defines the function, taking no parameters and returning no values.
  • Line 3 defines the rec record witht the t_cusrec type defined in the top of the module.
  • Line 7 declares the DISPLAY ARRAY dialog, by using the custarr dynamic array defined as module variable (available in this scope), and binding the structured array to the form fields grouped in the sa_cust screen array.
  • Line 8: UNBUFFERED is best practice in this context.
  • Line 9: We do not need the "cancel" action created implictly for DISLPAY ARRAY dialogs. We only want to keep the "accept" (OK button) action.
  • Lines 12 thru 26 define the ON APPEND block. Go to The ON APPEND trigger for more details.
  • Lines 28 thru 46 define the ON UPDATE block. Go to The ON UPDATE trigger for more details.
  • Lines 48 thry 59 define the ON DELETE block. Go to The ON DELETE trigger for more details.