Example: custlist.4gl
The custlist.4gl module defines a 'zoom' module, to let the user select a customer from a list. The module could be reused for any application that requires the user to select a customer from a list.
The custlist.4gl module header:
1 SCHEMA custdemo
2
3 PRIVATE TYPE t_cust RECORD
4 cust_num LIKE customer.cust_num,
5 cust_name LIKE customer.cust_name,
6 city LIKE customer.city
7 END RECORD
8
9 PRIVATE DEFINE custarr DYNAMIC ARRAY OF t_cust
10
11 PRIVATE FUNCTION custarr_fill(sql_cond STRING) RETURNS INTEGER
12 DEFINE sql_text STRING,
13 rec t_cust,
14 x INTEGER
15
16 LET sql_text = "SELECT cust_num, cust_name, city FROM customer"
17 IF sql_cond IS NOT NULL THEN
18 LET sql_text = sql_text || " WHERE " || sql_cond
19 END IF
20 LET sql_text = sql_text || " ORDER BY cust_name"
21
22 DECLARE c_cust CURSOR FROM sql_text
23
24 CALL custarr.clear()
25 FOREACH c_cust INTO rec.*
26 LET x = x + 1
27 LET custarr[x] = rec
28 END FOREACH
29
30 RETURN custarr.getLength()
31
32 END FUNCTION
33
34 PUBLIC FUNCTION select_customer() RETURNS (INTEGER,STRING)
35
36 DEFINE sql_cond STRING
37 DEFINE ret_num LIKE customer.cust_num
38 DEFINE ret_name LIKE customer.cust_name
39 DEFINE x INTEGER
40 DEFINE cnt INTEGER
41
42 OPEN WINDOW w_cust WITH FORM "custlist"
43
44 DIALOG ATTRIBUTES(UNBUFFERED)
45
46 CONSTRUCT BY NAME sql_cond ON customer.cust_name
47 END CONSTRUCT
48
49 DISPLAY ARRAY custarr TO sa_cust.*
50 END DISPLAY
51
52 BEFORE DIALOG
53 LET cnt = custarr_fill("1 = 1")
54
55 ON ACTION fetch ATTRIBUTES(ACCELERATOR="F5")
56 LET cnt = custarr_fill(sql_cond)
57 IF cnt == 0 THEN
58 NEXT FIELD cust_name
59 ELSE
60 NEXT FIELD s_num
61 END IF
62
63 ON ACTION accept
64 LET x = DIALOG.getCurrentRow("sa_cust")
65 IF x > 0 THEN
66 LET ret_num = custarr[x].cust_num
67 LET ret_name = custarr[x].cust_name
68 EXIT DIALOG
69 END IF
70
71 ON ACTION cancel
72 EXIT DIALOG
73
74 END DIALOG
75
76 CLOSE WINDOW w_cust
77
78 RETURN ret_num, ret_name
79
80 END FUNCTION
Note:
- Lines
1
declares the database schema forLIKE
instructions. - Lines
3
thru7
define thet_cust
type with the customer number, name and city fields, using the same data type than thecust_num
,cust_name
andcity
columns of thestock
table. - Line
9
defines the dynamic array in the scope of the module, that holds the list of customers fetched from the database table. - Line
34
declares theselect_customer
function, with no parameters. The function returns an integer: this is the stock number selected by the user. - Lines
9
thru12
declare local function variables. - Line
14
initializes theret_num
variable to -1: If the user cancels the list dialog, the code at line34
will not be executed and the function will return -1, 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
26
opens a new window with thestocklist
form. - Lines
28
thru36
implement theDISPLAY ARRAY
dialog to let the user choose a stock item from the list. - 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. - Line
38
closes the window used by the function. - Line
40
returns the value hold in theret_num
variable. A positive value identifies the stock item number selected by the user, while -1 means the chooce was canceled.
The
custarr_fill
function: 1 PRIVATE FUNCTION custarr_fill(sql_cond STRING) RETURNS INTEGER
2 DEFINE sql_text STRING,
3 rec t_cust,
4 x INTEGER
5
6 LET sql_text = "SELECT cust_num, cust_name, city FROM customer"
7 IF sql_cond IS NOT NULL THEN
8 LET sql_text = sql_text || " WHERE " || sql_cond
9 END IF
10 LET sql_text = sql_text || " ORDER BY cust_name"
11
12 DECLARE c_cust CURSOR FROM sql_text
13
14 CALL custarr.clear()
15 FOREACH c_cust INTO rec.*
16 LET x = x + 1
17 LET custarr[x] = rec
18 END FOREACH
19
20 RETURN custarr.getLength()
21
22 END FUNCTION
Note:
- Lines
1
declares the database schema forLIKE
instructions. - Lines
3
thru7
define thet_cust
type with the customer number, name and city fields, using the same data type than thecust_num
,cust_name
andcity
columns of thestock
table. - Line
9
defines the dynamic array in the scope of the module, that holds the list of customers fetched from the database table. - Line
34
declares theselect_customer
function, with no parameters. The function returns an integer: this is the stock number selected by the user. - Lines
9
thru12
declare local function variables. - Line
14
initializes theret_num
variable to -1: If the user cancels the list dialog, the code at line34
will not be executed and the function will return -1, 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
26
opens a new window with thestocklist
form. - Lines
28
thru36
implement theDISPLAY ARRAY
dialog to let the user choose a stock item from the list. - 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. - Line
38
closes the window used by the function. - Line
40
returns the value hold in theret_num
variable. A positive value identifies the stock item number selected by the user, while -1 means the chooce was canceled.
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 OPEN WINDOW w_cust WITH FORM "custlist"
10
11 DIALOG ATTRIBUTES(UNBUFFERED)
12
13 CONSTRUCT BY NAME sql_cond ON customer.cust_name
14 END CONSTRUCT
15
16 DISPLAY ARRAY custarr TO sa_cust.*
17 END DISPLAY
18
19 BEFORE DIALOG
20 LET cnt = custarr_fill("1 = 1")
21
22 ON ACTION fetch ATTRIBUTES(ACCELERATOR="F5")
23 LET cnt = custarr_fill(sql_cond)
24 IF cnt == 0 THEN
25 NEXT FIELD cust_name
26 ELSE
27 NEXT FIELD s_num
28 END IF
29
30 ON ACTION accept
31 LET x = DIALOG.getCurrentRow("sa_cust")
32 IF x > 0 THEN
33 LET ret_num = custarr[x].cust_num
34 LET ret_name = custarr[x].cust_name
35 EXIT DIALOG
36 END IF
37
38 ON ACTION cancel
39 EXIT DIALOG
40
41 END DIALOG
42
43 CLOSE WINDOW w_cust
44
45 RETURN ret_num, ret_name
46
47 END FUNCTION
Note:
- Lines
1
declares the database schema forLIKE
instructions. - Lines
3
thru7
define thet_cust
type with the customer number, name and city fields, using the same data type than thecust_num
,cust_name
andcity
columns of thestock
table. - Line
9
defines the dynamic array in the scope of the module, that holds the list of customers fetched from the database table. - Line
34
declares theselect_customer
function, with no parameters. The function returns an integer: this is the stock number selected by the user. - Lines
9
thru12
declare local function variables. - Line
14
initializes theret_num
variable to -1: If the user cancels the list dialog, the code at line34
will not be executed and the function will return -1, 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
26
opens a new window with thestocklist
form. - Lines
28
thru36
implement theDISPLAY ARRAY
dialog to let the user choose a stock item from the list. - 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. - Line
38
closes the window used by the function. - Line
40
returns the value hold in theret_num
variable. A positive value identifies the stock item number selected by the user, while -1 means the chooce was canceled.