Example: dispcust.4gl (function query_cust)
This function retrieves a row from the customer
table and displays
it in a form.
Function
query_cust
:01
FUNCTION
query_cust() -- displays one row
02
DEFINE
l_custrec RECORD
03
store_num LIKE
customer.store_num,
04
store_name LIKE
customer.store_name,
05
addr LIKE
customer.addr,
06
addr2 LIKE
customer.addr2,
07
city LIKE
customer.city,
08
state LIKE
customer.state,
09
zip_code LIKE
customer.zip_code,
10
contact_name LIKE
customer.contact_name,
11
phone LIKE
customer.phone
12
END RECORD
13
14
SELECT
store_num,
15
store_name,
16
addr,
17
addr2,
18
city,
19
state,
20
zip_code,
21
contact_name,
22
phone
23
INTO
l_custrec.*
24
FROM
customer
25
WHERE
store_num = 101
26
27
DISPLAY BY NAME
l_custrec.*
28
MESSAGE
"Customer " || l_custrec.store_num ||
29
" displayed."
30
END FUNCTION
Note:
- Line
01
is the beginning of the functionquery_cust
. No variables are passed to the function. - Lines
02
thru12
DEFINE
a recordl_custrec
asLIKE
columns in thecustomer
database table, listing each variable separately. - Line
14
thru25
SELECT ... INTO
can be used, since the statement will retrieve only one row from the database. TheSELECT
statement lists each column name to be retrieved, rather than usingSELECT *
. This allows for the possibility that additional columns might be added to a table at a future date. Since theSELECT
list retrieves values for all the variables in the program record, in the order listed in theDEFINE
statement, the shorthandINTO l_custrec.*
can be used. - Line
27
The names in the program recordl_custrec
match the names of screen fields on the form, soDISPLAY BY NAME
can be used.l_custrec.*
indicates that all of the members of the program record are to be displayed. - Lines
28
and29
A string for theMESSAGE
statement is concatenated together using the double pipe ( || ) operator and displayed. The message consists of the string "Customer", the value ofl_custrec.store_num
, and the string "displayed".
There are no additional statements in the function, so the program returns to the
MENU
statement, awaiting the user's next action.