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
001defines the database schema to be used by this module. - Lines
003thru007define thecust_t TYPEas aRECORDwith three members declared with aLIKEreference to the database column. - Line
009defines thecust_arrprogram array with the type defined in previous lines. - Lines
011thru029define thecustlist_fill()function which fillscust_arrwith the values of database rows.- Lines
016thru020declare thecustlist_cursSQL cursor by using thewhere_clausecondition passed as the parameter. - Lines
022thru027fetch the database rows intocust_arr.
- Lines
- Lines
031thru074implement thedisplay_custlist()function to be called by the main module.- Lines
040and041initialize theret_numandret_namevariables. If the user cancels the dialog, the function will return these values to let the caller decide what to do. - Lines
043thru068define aDIALOGinstruction implementing the controller of the form.- Lines
045thru046define theCONSTRUCTsub-dialog controlling thecustomer.store_namequery field. - Lines
048thru049define theDISPLAY ARRAYsub-dialog controlling thesa_custscreen array. - Lines
051thru052implement theBEFORE DIALOGtrigger, to fill the list with an initial result set by passing the query criteria as "1 =1" to thecust_list_fill()function. - Lines
054thru055implement thefetchON ACTIONtrigger, executed when the user presses thefebutton in the form, to fill the list with a result set by passing the query criteria inwhere_clauseto thecust_list_fillfunction. - Lines
057thru063implement theacceptON ACTIONtrigger, 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_numandret_namewith the current row. - Lines
065thru066implement thecancelON ACTIONtrigger, to leave the dialog when the user hits the Cancel button.
- Lines
- Line
072returns the values of theret_numandret_namevariables.
- Lines