Module custquery.4gl (function inpupd_cust)

A new function, inpupd_cust, is added to the custquery.4gl module, allowing the user to insert values for a new customer row into the form.

Function inpupd_cust (custquery.4gl):
01 FUNCTION inpupd_cust(au_flag)  
02   DEFINE au_flag CHAR(1),
03          cont_ok SMALLINT
04
05   LET cont_ok = TRUE 
06
07
08   IF (au_flag = "A") THEN
09     MESSAGE "Add a new customer"
10     INITIALIZE mr_custrec.* TO NULL
12   END IF
13
14   LET INT_FLAG = FALSE
15
16   INPUT BY NAME mr_custrec.*
17         WITHOUT DEFAULTS ATTRIBUTES(UNBUFFERED) 
18
19   ON CHANGE store_num 
20     IF (au_flag = "A") THEN
21       SELECT store_name, 
22              addr,
23              addr2, 
24              city, 
25              state, 
26              zip_code,
27              contact_name, 
28              phone 
29         INTO mr_custrec.*
30         FROM customer 
31         WHERE store_num = mr_custrec.store_num 
32       IF (SQLCA.SQLCODE = 0)THEN
33         ERROR "Store number already exists."
34         LET cont_ok = FALSE
35         CALL display_cust()
36         EXIT INPUT
37       END IF
38     END IF
39
40   AFTER FIELD store_name 
41     IF (mr_custrec.store_name IS NULL) THEN
42       ERROR "You must enter a company name."
43       NEXT FIELD store_name 
44     END IF
45
46   END INPUT
47
48   IF (INT_FLAG) THEN
49     LET INT_FLAG = FALSE
50     LET cont_ok = FALSE
51     MESSAGE "Operation cancelled by user"
52     INITIALIZE mr_custrec.* TO NULL
53   END IF
54  
55   RETURN cont_ok 
56
57 END FUNCTION
  • Line 01 The function accepts a parameter defined as CHAR(1). In order to use the same function for both the input of a new record and the update of an existing one, the CALL to this function in the MENU statement in main.4gl will pass a value "A" for add, and "U" for update.
  • Line 06 The variable cont_ok is a flag to indicate whether the update operation should continue; set initially to TRUE.
  • Lines 08 thru 12 test the value of the parameter au_flag. If the value of au_flag is "A" the operation is an Add of a new record, and a MESSAGE is displayed. Since this is an Add, the modular program record values are initialized to NULL prior to calling the INPUT statement, so the user will have empty form fields in which to enter data.
  • Line 14 sets the INT_FLAG global variable to FALSE prior to the INPUT statement, so the program can determine if the user cancels the dialog.
  • Line 17 The UNBUFFERED and WITHOUT DEFAULTS clauses of the INPUT statement are used. The UNBUFFERED attribute insures that the program array the screen array of the form are automatically synchronized for input and output. The WITHOUT DEFAULTS clause is used since this statement will also implement record updates, to prevent the existing values displayed on the form from being erased or replaced with default values.
  • Lines 19 thru 38 Each time the value in store_num changes, the customer table is searched to see if that store_num already exists. If so, the values in the mr_custrec record are displayed in the form, the variable cont_ok is set to FALSE, and the INPUT statement is immediately terminated.
  • Lines 40 thru 44 The AFTER FIELD control block verifies that store_name was not left blank. If so, the NEXT FIELD statement returns the focus to the store_name field so the user may enter a value.
  • Line 46 END INPUT is required when any of the optional control blocks of the INPUT statement are used.
  • Lines 48 thru 53 The INT_FLAG is checked to see if the user has canceled the input. If so, the variable cont_ok is set to FALSE, and the program record mr_custrec is set to NULL . The UNBUFFERED attribute of the INPUT statement assures that the NULL values in the program record are automatically displayed on the form.
  • Line 55 returns the value of cont_ok, indicating whether the input was successful.