When you define a browse, you must associate it with a previously defined query. This association allows the AVM to identify the source for the browse columns and other information. However, at run time you can associate a browse with any query that supplies the columns the browse needs by setting the browse’s QUERY attribute to the handle of the query. The query must supply the same columns as the one you defined the browse with.

When you set the QUERY attribute, the AVM immediately refreshes the browse with the results of the new query. If the query is not open then the AVM empties the browse.

You can use this capability to define a placeholder query for a browse simply to satisfy the definition, and then to associate the actual query with it at run time. Or you can use this to alternate between two or more queries to display different useful sets of data in the same browse.

To define a different query on the Order table to alternate with the display of Customers of the current Order:

(This alternative shows all the Orders in the database that have the same OrderDate as the currently displayed Order.)

  1. In the Definitions section of h-CustOrderWin5.w, add these lines:
    DEFINE BUFFER bOrder2 FOR Order.
    DEFINE QUERY qOrderDate FOR bOrder2 SCROLLING.
    DEFINE VARIABLE lOrderDate AS LOGICAL NO-UNDO.

    Buffer bOrder2 is a second buffer for the Order table and query qOrderDate is a query on this buffer. You use this to define a query for other Orders that have the same OrderDate as the currently displayed Order. The lOrderDate flag tells the program whether the user is currently seeing the results of the query on the OrderDate or not.

  2. Add a button to the window named btnOrderDate with the label Show all on this Date.
  3. Define this CHOOSE trigger for the button:
    DO:
      IF NOT lOrderDate THEN
        DO:
          /* If the standard query is displayed, open the query for
             Orderdates and make that the browse's query.
             Hide the OrderLine browse while we're
             doing this, and adjust the button label accordingly. */
          OPEN QUERY qOrderDate FOR EACH bOrder2
            WHERE bOrder2.OrderDate = Order.OrderDate.
          ASSIGN BROWSE OrderBrowse:QUERY = QUERY qOrderDate:HANDLE
            /* Signal that we're showing the OrderDate query. */
            lOrderDate = TRUE
            hBrowse:HIDDEN = TRUE
            SELF:LABEL = "Show Customer's Orders".
        END.
      ELSE
        /* If we're showing the OrderDate query, switch back to the
           regular query for the current Customer's Orders. */
        ASSIGN BROWSE OrderBrowse:QUERY = QUERY OrderBrowse:HANDLE
          lOrderDate = FALSE
          hBrowse:HIDDEN = FALSE
          SELF:LABEL = "Show all on this Date".  
    END.
    If the lOrderDate flag is not set, then the user sees the default query of Orders for the current Customer. In this case the code:
    1. Opens the other query for all Orders matching the current OrderDate
    2. Assigns this to be the query for the OrderBrowse
    3. Reverses the value of the flag variable
    4. Hides the OrderLine browse because it is not relevant to this display
    5. Switches the label of the button to allow the user to change back to the original query

    If the flag is set, then the trigger reverts to the original query and label, resets the flag, and views the OrderLine browse.

    There is one more step you need to take, so that the procedure always reverts back to the original query if the user clicks one of the First, Next, Last, or Prev buttons.

  4. Create a local copy of the h-ButtonTrig1.i include file, call it h-ButtonTrig2.i, and add these lines to it:
    /* h-ButtonTrig2.i -- include file for the First/Next/Prev/Last buttons
      in h-CustOrderWin5.w. */
    GET {1} CustQuery.
    IF AVAILABLE Customer THEN
      DISPLAY Customer.CustNum Customer.Name Customer.Address Customer.City
        Customer.State
        WITH FRAME CustQuery IN WINDOW CustWin.
    IF lOrderDate THEN
        APPLY "CHOOSE" TO btnOrderDate.
    {&OPEN-BROWSERS-IN-QUERY-CustQuery}
    APPLY "VALUE-CHANGED" TO OrderBrowse.

    This code runs the trigger block if the flag is true, so that it reverts to the Orders OF Customer query.

  5. Run the procedure now and click the Show all on this Date button. You can see the effect of switching queries for the browse: