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 FUNCTIONNote:
- Line
1declares theselect_customerfunction, 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
3thru7declare local function variables. - Line
9initializes theret_numvariable to -1: If the user cancels the list dialog, the code at lines35and36will not be executed and the function will return -1 andNULL, indicating that the user has canceled the choice. - Lines
16thru24use an SQL cursor to fetch rows from thestockSQL table into thestockarrprogram variable. - Line
11opens a new window with thecustlistform. - Lines
13thru43implement theDIALOGmultiple-dialog with aCONTRUCTandDISPLAY ARRAYsub-dialogs, to let the user filter rows from the customer table, and choose a customer from the list. - Lines
15thru16implement theCONSTRUCTsub-dialog, that fills the sql_cond string variable with the SQL condition corresponding to the criteria entered in thecust_namefield. - Lines
18thru19implement theDISPLAY ARRAYsub-dialog, to control theTABLEcontainer of the form showing the customer list, from the data rows of thecustarrdynamic array. - Lines
34thru35check 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
24thru30declare an action handler for the"fetch"action, that takes the SQL condition produced by theCONSTRUCT, to re-fill the program array by calling thecustarr_fillfunction. 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 theTABLEcontainer with aNEXT FIELD s_numinstruction. - Lines
32thru38implement the action handler to validate the dialog, for the OK button. If there is a current row, we set theret_numandret_namevariables with the number and name of the current customer row. - Lines
40thru41define the action handler to cancel the dialog, for the Cancel button. - Line
38closes the window used by the function. - Line
47returns the value hold in theret_numandret_namevariables. A positive number identifies the customer number selected by the user, while -1 means that the choice was canceled.