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 FUNCTIONNote:
- Line
13A single line is added to thequery_custfunction to return the value ofcont_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 MAINNote:
- Line
03defines the variablequery_ok, which will be used to indicate whether the user has queried. - Line
09sets the initial value ofquery_oktoFALSE. - Line
13the functionquery_custnow returns a value forquery_ok. - Lines
15thru19and Lines21thru25:these sections test the value ofquery_okwhen Next or Previous has been selected. Ifquery_okisTRUE, the functionfetch_rel_custis called; otherwise, a message is displayed to the user. - Line
31calls the cleanup function to close the cursor used to fetch the database rows.