Tutorial Chapter 4: Query by Example / Modifying the Program to Handle Errors |
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.
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
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