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 theselect_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
thru7
declare local function variables. - Line
9
initializes theret_num
variable to -1: If the user cancels the list dialog, the code at lines35
and36
will not be executed and the function will return -1 andNULL
, indicating that the user has canceled the choice. - Lines
16
thru24
use an SQL cursor to fetch rows from thestock
SQL table into thestockarr
program variable. - Line
11
opens a new window with thecustlist
form. - Lines
13
thru43
implement theDIALOG
multiple-dialog with aCONTRUCT
andDISPLAY ARRAY
sub-dialogs, to let the user filter rows from the customer table, and choose a customer from the list. - Lines
15
thru16
implement theCONSTRUCT
sub-dialog, that fills the sql_cond string variable with the SQL condition corresponding to the criteria entered in thecust_name
field. - Lines
18
thru19
implement theDISPLAY ARRAY
sub-dialog, to control theTABLE
container of the form showing the customer list, from the data rows of thecustarr
dynamic array. - Lines
34
thru35
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
thru30
declare an action handler for the"fetch"
action, that takes the SQL condition produced by theCONSTRUCT
, to re-fill the program array by calling thecustarr_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 withNEXT FIELD cust_name
. Otherwise, when rows are found, we put the focus into theTABLE
container with aNEXT FIELD s_num
instruction. - Lines
32
thru38
implement the action handler to validate the dialog, for the OK button. If there is a current row, we set theret_num
andret_name
variables with the number and name of the current customer row. - Lines
40
thru41
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 theret_num
andret_name
variables. A positive number identifies the customer number selected by the user, while -1 means that the choice was canceled.