The query is sorted by the City, which is a nonindexed field. Therefore, the NUM-RESULTS function returns the total number of records that satisfy the query as soon as it is opened, because they all have to be retrieved before they can be sorted.

Next, you add another field to the screen to display that number, and then check the same value in the trigger for the New State field to make sure that the state entered was valid.

To add another field to the screen:

  1. Drop another fill-in field onto the window. Name it iMatches and give it a label of Number of Matches.
  2. Open the Property Sheet for iMatches and change the type to Integer .
  3. To display initial values for the New State and Number of Matches fields, you put some code into the Main Block of the procedure, to be executed right after the query is initially opened.

    In the Section Editor, select the Main Block and add this code after the RUN enable_UI statement:

    DO ON ERROR UNDO MAIN-BLOCK, LEAVE MAIN-BLOCK
      ON END-KEY UNDO MAIN-BLOCK, LEAVE MAIN-BLOCK:
      RUN enable_UI.
      ASSIGN
        cState = Customer.State
        iMatches = NUM-RESULTS("CustQuery").
      DISPLAY cState iMatches WITH FRAME CustQuery.
    ...
    END.

    This displays the initial value of the Customer State (“NH”) along with the number of Customers in New Hampshire. Remember that, in this case, CustQuery is both the name of the query and also the name of the frame the AppBuilder created for you.

  4. Edit the LEAVE trigger for the New State field again. Add a statement before the OPEN QUERY statement to save off the current State in the cState field, and a statement following the OPEN QUERY statement to retrieve the NUM-RESULTS value for the new query:
      cState = Customer.State.
      OPEN QUERY CustQuery FOR EACH Customer WHERE Customer.State =
        cState:SCREEN-VALUE BY Customer.City.
      /* Check whether the State was valid or not. */
      iMatches = NUM-RESULTS("CustQuery").
    ...

    Remember that the value you assign to cState is not overwritten by the SCREEN-VALUE that you type into the field on the screen, so you can open the query based on the SCREEN-VALUE and still keep the old value around if you need it later (which you will).