Example: custquery.4gl (Function query_cust)

This module of the Query program contains the logic for querying the database and displaying the data retrieved.

The module declares that it needs to use the custdemo database schema, and declares a structured variable with a local module scope, to hold the values of a customer record:
  1 SCHEMA custdemo
  2 
  3 DEFINE mr_custrec RECORD LIKE customer.*

The function query_cust is called by the main module:

  1 FUNCTION query_cust() RETURNS ()
  2   DEFINE cust_cnt INTEGER,
  3          where_clause STRING
  4          
  5   LET int_flag = FALSE
  6   
  7   CONSTRUCT BY NAME where_clause ON customer.*
  8   
  9   IF int_flag THEN
 10     LET int_flag=FALSE
 11     CLEAR FORM
 12     MESSAGE "Canceled by user."
 13     RETURN
 14   ELSE
 15     LET cust_cnt = get_cust_cnt(where_clause)
 16     IF cust_cnt > 0 THEN
 17        MESSAGE SFMT("%1 rows found.",cust_cnt)
 18        IF cust_select(where_clause) THEN
 19           CALL display_cust()
 20        END IF
 21     ELSE
 22        MESSAGE "No rows found."
 23     END IF
 24   END IF
 25   
 26 END FUNCTION
Note:
  • Line 1: Function query_cust. This is the beginning of the function query_cust.
  • Line 2 defines cust_cnt, a local variable of data type INTEGER, to hold the number of rows returned by the SELECT statement.
  • Line 3 defines where_clause as a local STRING variable to hold the boolean condition resulting from the CONSTRUCT statement.
  • Line 5 sets int_flag to FALSE. It is common to set this global flag to FALSE immediately prior to the execution of an interactive dialog, so your program can test whether the user attempted to cancel the dialog.
  • Lines 7: The CONSTRUCT statement defines the form fields for which the user may enter search criteria. The BY NAME syntax matches the database columns to form fields having the same name.
  • Line 9 is the beginning of an IF statement testing the value of int_flag. This test appears immediately after the CONSTRUCT statement, to test whether the user terminated the CONSTRUCT statement (int_flag would be set by the runtime system to TRUE).
  • Lines 10 thru 13 are executed only if the value of int_flag is TRUE. The int_flag is immediately reset to FALSE, since it is a global variable which other parts of your program will test. The form is cleared of any criteria that the user has entered. Then the RETURN instruction terminates the function and we return to the caller block.
  • Lines 14 thru 24: contain the logic to be executed if int_flag was not set to TRUE (the user hit the OK button to execute the query).
    • In lines 15, the get_cust_cnt function is called, to retrieve the number of rows that would be returned by the query criteria. The where_clause variable is passed to the function, and the value returned will be stored in the cust_cnt variable.
    • Lines 16 is the beginning of a nested IF statement, testing the value of cust_cnt.
    • Lines 17 thru 20 are executed if the value of cust_cnt is greater than zero; a message with the number of rows returned is displayed to the user, and the function cust_select is called, and the display_cust function is called if cust_select returns TRUE.
    • Lines 22 is executed if the cust_cnt is zero (no rows found); a message is displayed to the user.