The Customer List Module
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.
This module uses the custlist.per form and is implemented with a
DIALOG
instruction defining a CONSTRUCT
sub-dialog
and a DISPLAY ARRAY
sub-dialog. The display_custlist()
function in this module returns the customer id and the name.
In the application illustrated in this chapter, the main module orders.4gl
will call the display_custlist()
function to retrieve a
customer selected by the user.
01
ON ACTION
zoom1
02
CALL
display_custlist() RETURNING
id, name
03
IF
(id > 0) THEN
04
...
Here is the complete source code.
Module custlist.4gl:
001
SCHEMA
custdemo
002
003
TYPE
cust_t RECORD
004
store_num LIKE
customer.store_num,
005
store_name LIKE
customer.store_name,
006
city LIKE
customer.city
007
END RECORD
008
009
DEFINE
cust_arr DYNAMIC ARRAY OF
cust_t
010
011
FUNCTION
custlist_fill(where_clause)
012
DEFINE
where_clause STRING
013
DEFINE
idx SMALLINT
014
DEFINE
cust_rec cust_t
015
016
DECLARE
custlist_curs CURSOR FROM
017
"SELECT store_num, store_name, city "||
018
" FROM customer"||
019
" WHERE "||where_clause||
020
" ORDER BY store_num"
021
022
LET
idx = 0
023
CALL
cust_arr.clear()
024
FOREACH
custlist_curs INTO
cust_rec.*
025
LET
idx = idx + 1
026
LET
cust_arr[idx].* = cust_rec.*
027
END FOREACH
028
029
END FUNCTION
030
031
FUNCTION
display_custlist()
033
DEFINE
ret_num LIKE
customer.store_num
034
DEFINE
ret_name LIKE
customer.store_name
035
DEFINE
where_clause STRING
036
DEFINE
idx SMALLINT
037
038
OPEN WINDOW
wcust WITH FORM
"custlist"
039
040
LET
ret_num = 0
041
LET
ret_name = NULL
042
043
DIALOG ATTRIBUTES
(UNBUFFERED
)
044
045
CONSTRUCT BY NAME
where_clause ON
customer.store_name
046
END CONSTRUCT
047
048
DISPLAY ARRAY
cust_arr TO
sa_cust.*
049
END DISPLAY
050
051
BEFORE DIALOG
052
CALL
custlist_fill("1 = 1")
053
054
ON ACTION
fetch
055
CALL
custlist_fill(where_clause)
056
057
ON ACTION
accept
058
LET
idx = DIALOG.getCurrentRow("sa_cust")
059
IF
idx > 0 THEN
060
LET
ret_num = cust_arr[idx].store_num
061
LET
ret_name = cust_arr[idx].store_name
062
EXIT DIALOG
063
END IF
064
065
ON ACTION
cancel
066
EXIT DIALOG
067
068
END DIALOG
069
070
CLOSE WINDOW
wcust
071
072
RETURN
ret_num, ret_name
073
074
END FUNCTION
Note:
- Line
001
defines the database schema to be used by this module. - Lines
003
thru007
define thecust_t TYPE
as aRECORD
with three members declared with aLIKE
reference to the database column. - Line
009
defines thecust_arr
program array with the type defined in previous lines. - Lines
011
thru029
define thecustlist_fill()
function which fillscust_arr
with the values of database rows.- Lines
016
thru020
declare thecustlist_curs
SQL cursor by using thewhere_clause
condition passed as the parameter. - Lines
022
thru027
fetch the database rows intocust_arr
.
- Lines
- Lines
031
thru074
implement thedisplay_custlist()
function to be called by the main module.- Lines
040
and041
initialize theret_num
andret_name
variables. If the user cancels the dialog, the function will return these values to let the caller decide what to do. - Lines
043
thru068
define aDIALOG
instruction implementing the controller of the form.- Lines
045
thru046
define theCONSTRUCT
sub-dialog controlling thecustomer.store_name
query field. - Lines
048
thru049
define theDISPLAY ARRAY
sub-dialog controlling thesa_cust
screen array. - Lines
051
thru052
implement theBEFORE DIALOG
trigger, to fill the list with an initial result set by passing the query criteria as "1 =1" to thecust_list_fill()
function. - Lines
054
thru055
implement thefetch
ON ACTION
trigger, executed when the user presses thefe
button in the form, to fill the list with a result set by passing the query criteria inwhere_clause
to thecust_list_fill
function. - Lines
057
thru063
implement theaccept
ON ACTION
trigger, executed when the user validates the dialog with the OK button or with a double-click in a row of the list. The code initializes the return valuesret_num
andret_name
with the current row. - Lines
065
thru066
implement thecancel
ON ACTION
trigger, to leave the dialog when the user hits the Cancel button.
- Lines
- Line
072
returns the values of theret_num
andret_name
variables.
- Lines