Error if Cursor is not Open

In the example program in this chapter, if the user selects the Next or Previous action from the MENU before he has queried, the program returns an error ("Program stopped at line .... Fetch attempted on unopened cursor").

One way to prevent this error would be to add a variable to the program to indicate whether the user has queried for a result set, and to prevent him from executing the actions associated with Next or Previous until he has done so.

Changes to function query_cust (custquery.4gl):
01 FUNCTION query_cust()
02   DEFINE cont_ok SMALLINT,
03         cust_cnt SMALLINT,
04         where_clause STRING
05   MESSAGE "Enter search criteria"
06   LET cont_ok = FALSE
07
...
08
09   IF (cont_ok = TRUE) THEN
10      CALL display_cust()
11   END IF
12    
13   RETURN cont_ok 
14
15 END FUNCTION
Note:
  • Line 13 A single line is added to the query_cust function to return the value of cont_ok, which indicates whether the query was successful, to the calling function in custmain.4gl.
Changes to module custmain.4gl:
01 MAIN
02   DEFINE query_ok SMALLINT
03 
04   DEFER INTERRUPT
05
06   CONNECT TO "custdemo"
07   CLOSE WINDOW SCREEN
08   OPEN WINDOW w1 WITH FORM "custform"
09   LET query_ok = FALSE
10
11   MENU "Customer"
12     ON ACTION query 
13       CALL query_cust() RETURNING query_ok 
14     ON ACTION next           
15       IF (query_ok) THEN
16         CALL fetch_rel_cust(1)
17       ELSE
18         MESSAGE "You must query first."
19       END IF
20     ON ACTION previous 
21       IF (query_ok) THEN
22         CALL fetch_rel_cust(-1)
23       ELSE
24         MESSAGE "You must query first."
25       END IF
26     ON ACTION quit 
27       EXIT MENU
28   END MENU
29
30   CLOSE WINDOW w1
31   CALL cleanup()
32   DISCONNECT CURRENT
33
34 END MAIN
Note:
  • Line 03 defines the variable query_ok, which will be used to indicate whether the user has queried.
  • Line 09 sets the initial value of query_ok to FALSE.
  • Line 13 the function query_cust now returns a value for query_ok.
  • Lines 15 thru 19 and Lines 21 thru 25: these sections test the value of query_ok when Next or Previous has been selected. If query_ok is TRUE, the function fetch_rel_cust is called; otherwise, a message is displayed to the user.
  • Line 31 calls the cleanup function to close the cursor used to fetch the database rows.