Example: stocklist.4gl
The stocklist.4gl module defines a 'zoom' module, to let the user select a stock item from a list.
The stocklist.4gl module:
1 SCHEMA custdemo
2
3 PRIVATE TYPE t_stock RECORD
4 stock_num LIKE stock.stock_num,
5 description LIKE stock.description
6 END RECORD
7
8 PUBLIC FUNCTION select_stock_item() RETURNS INTEGER
9 DEFINE stockarr DYNAMIC ARRAY OF t_stock,
10 rec t_stock,
11 ret_num LIKE stock.stock_num,
12 x INTEGER
13
14 LET ret_num = -1
15
16 DECLARE c_stock CURSOR FOR
17 SELECT stock_num, description
18 FROM stock
19 ORDER BY stock_num
20
21 FOREACH c_stock INTO rec.*
22 LET x = x + 1
23 LET stockarr[x] = rec
24 END FOREACH
25
26 OPEN WINDOW w_stock WITH FORM "stocklist"
27
28 LET int_flag = FALSE
29 DISPLAY ARRAY stockarr TO sa_stock.*
30 BEFORE DISPLAY
31 CALL DIALOG.setActionActive("accept",(stockarr.getLength()>0))
32 AFTER DISPLAY
33 IF NOT int_flag THEN
34 LET ret_num = stockarr[arr_curr()].stock_num
35 END IF
36 END DISPLAY
37
38 CLOSE WINDOW w_stock
39
40 RETURN ret_num
41
42 END FUNCTION
Note:
- Lines
1
declares the database schema forLIKE
instructions. - Lines
3
thru6
define thet_stock
type with the stock number and description fields, using the same data type than thestock_num
anddescrition
columns of thestock
table. - Line
8
declares theselect_stock_item
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 that the choice was canceled.