Add scripts to retrieve data for the DataWindow controls

Note

The scripts you just typed have no effect on the dw_master DataWindow control, but now that you have a script for the ue_retrieve event, you need only trigger this event to retrieve data into the dw_master DataWindow.

You trigger the ue_retrieve event from the sheet window Open event. This retrieves data into the dw_master DataWindow as soon as the window (or one of its descendent windows) opens. Then you add a script for the RowFocusChanged event of dw_master to retrieve data into the dw_detail DataWindow. The RowFocusChanged event is triggered each time the focus is changed inside the dw_master DataWindow.

NoteRowFocusChanged occurs upon DataWindow display The RowFocusChanged event also occurs when the w_master DataWindow is first displayed. This allows the application to retrieve and display detail information for the first row retrieved in the master DataWindow.

Here is how the script works for the w_master_detail_ancestor window and its descendants when you are done:

  1. Select open from the second drop-down list box in the Script view for w_master_detail_ancestor.

    The Open event has a purple script icon indicating it has an ancestor script. If you check the ancestor script, you see that it calls the ue_postopen event and posts it to the end of the window’s message queue.

  2. Type these new lines in the script area for the w_master_detail_ancestor Open event:

    dw_master.settransobject ( sqlca )
    
    dw_detail.settransobject ( sqlca )
    
    this.EVENT ue_retrieve()
    

    The first two lines tell the dw_master and dw_detail DataWindows to look in the SQLCA Transaction object for the values of the database variables. The third line triggers the ue_retrieve event. The pronoun This refers to the current object. In this example, the w_master_detail_ancestor window is the current object.

  3. Select dw_master in the first drop-down list box of the Script view.

    Select rowfocuschanged in the second drop-down list box.

    NoteRead the event name carefully Make sure you select the RowFocusChanged event, and not the RowFocusChanging event.

    You now add a script for the RowFocusChanged event of the dw_master DataWindow control. This script sends a retrieval request and the ID number of the selected row to the dw_detail DataWindow control.

  4. Type this line in the script area for the RowFocusChanged event:

    long ll_itemnum
    

    This line declares the local variable ll_itemnum (l is a letter, not a number), which has the long data type.

  5. Type this line below the variable declaration line you just typed:

    ll_itemnum = this.object.data[currentrow, 1]
    

    NoteUse square brackets The expression shown above requires square brackets, not parentheses.

    This line uses a DataWindow data expression to obtain the item number in column 1 of the currently selected row of dw_master. It stores the number in the variable ll_itemnum.

    CurrentRow is an argument passed to the RowFocusChanged event that specifies the current row in the DataWindow control. The current row is the row the user has selected by clicking or by scrolling with the arrow or tab keys.

  6. Type these lines below the data expression line you just typed:

    IF dw_detail.Retrieve(ll_itemnum) = -1 THEN	
    
     MessageBox("Retrieve","Retrieve error-detail")
    
    END IF
    

    This group of lines sends a retrieval request to the dw_detail DataWindow along with the argument the DataWindow expects (an ID number stored in the ll_itemnum variable). The IF statement that encloses the Retrieve function checks for successful completion. If the retrieval operation fails, it displays an error message box.

    Shown is the Script view for the row focus changed event with the following code: IF dw _ detail dot Retrieve ( l l _ item num ) equals minus 1 THEN Message Box ( " Retrieve " , " Retrieve error - detail " ) END IF
  7. Click the Save button in PainterBar1.

    Click the Close button in PainterBar1.

    PowerBuilder compiles the script you typed and saves it.

  8. Click the Full Build Workspace button in the PowerBar.

    It is a good idea to rebuild all your objects after modifying an ancestor object.

  9. Close the Output window.