Use NUM-RESULTS to check the validity of the query
- Last Updated: April 9, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
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:
- Drop another fill-in field onto the window. Name it
iMatchesand give it a label of Number of Matches. - Open the Property Sheet for
iMatchesand change the type to Integer . - To display initial values for the
New StateandNumber of Matchesfields, 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_UIstatement: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,
CustQueryis both the name of the query and also the name of the frame the AppBuilder created for you. - Edit the
LEAVEtrigger for the New State field again. Add a statement before theOPEN QUERYstatement to save off the current State in thecStatefield, and a statement following theOPEN QUERYstatement to retrieve theNUM-RESULTSvalue 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
cStateis not overwritten by theSCREEN-VALUEthat you type into the field on the screen, so you can open the query based on theSCREEN-VALUEand still keep the old value around if you need it later (which you will).