Displaying Data from a Set


This script demonstrates how you can use an Xdialog box to display data from a set. It shows data from the invoice.set in the AlphaSports sample application.

 The Main Script

First, dimension variables and open the set. Exit if the set cannot be found.

dim setname as C
dim vl as P
dim s as P
dim line_number as N
dim invoice_number as C
dim invoice_date as D
dim customer as C
dim lastname as C
dim firstname as C
dim address1 as C
dim address2 as C
dim city as C
dim state as C
dim zip as C
dim items as C
dim direction as C
dim title_bar as C
setname = a5.Get_Exe_Path()+ chr(92) + "samples\alphasports\invoice.set"
if file.exists(setname) = .f. then
    ui_msg_box("Error","Cannot run this script because '"+setname+"' not found.")
end if
vl = local_variables()
on error goto set_did_not_open
s = set.open_session(setname)

Next, open the parent invoice_header table and the child invoice_items table. Initialize variables.

on error goto 0
'get pointer to invoice_header table
tih = s.invoice_header
'get pointer to invoice_items table
tii = s.invoice_items
line_number = 1
items = ""
direction = ""
invoice_number = ""

Start creating the Xdialog with the navigation buttons. Note that the {can_exit} command allows you to close the dialog by clicking the X button at the upper right corner of the dialog. The {startup} command fires the init event, which displays the first records from the parent and child tables.

title_bar = "{F=Arial,8}{C=Dark Blue,White,Dark Blue}{B=White,Dark Blue,Blue White}#{T=0.25}Quantity{T=0.75}Description{T=3.25}Price{T=4.00}Total"

Create the fields to display the records from the parent table and the list box to display the records from the child table.

Invoice Number: |[.20invoice_number];
Date: |[.20invoice_date];
Name:|[.20firstname] [.32lastname];;
City, ST Zip: |[.31city] [.5state] [.12zip];
<10&Close!close> ;

This final block of the main script contains the event handling code, the error handler, and the end command.. Note how the INLIST()function is used to isolate the navigation events in a If ... END IF block.

if a_dlg_button = "init" then
    line_number = 1
end if
if a_dlg_button = "find" then
    line_number = 1
end if
if a_dlg_button = "close" then
end if
if inlist(a_dlg_button,"first","next","prev","last") then
    if a_dlg_button = "first" then
        direction = "first"
    else if a_dlg_button = "prev" then
        direction = "prev"
    else if a_dlg_button = "next" then
        direction = "next"
    else if a_dlg_button = "last" then
        direction = "last"
    end if
    line_number = 1
end if
ui_msg_box("Error",setname + " not found, or could not be opened.")

 Supporting Functions

The fetch_next function retrieves the appropriate invoice_header record, ignoring child records (because the fetch level is set to 1). It retrieves the record's field values with the get_header_variables function.

function fetch_next as C (vars as P)
    with vars
        if direction = "next" then
        else if direction = "prev" then
        else if direction = "last" then
        else if direction = "first" then
        end if
    end with
end function

The get_header_variables function reads the current invoice_header record and assigns the values to the fields of the Xdialog with the get_items function.

function get_header_variables as v (vars as P)
    with vars
        invoice_number = tih.eval("invoice_number")
        invoice_date = tih.eval("Date")
        lastname = tih.eval("customer->lastname")
        firstname = tih.eval("customer->firstname")
        company = tih.eval("customer->company")
        address1 = tih.eval("customer->Bill_Address_1")
        address2 = tih.eval("customer->Bill_Address_2")
        city = tih.eval("customer->Bill_City")
        state = tih.eval("customer->Bill_State_Region")
        zip = tih.eval("customer->Bill_Postal_Code")
    end with
end function

The get_items function builds a list of child records for the current invoice_header record. The function applies the formatting to the list that the Xdialog list box control requires with the A5_OWNER_DRAW_LIST_FMT()function.

function get_items as C (vars as P)
    Dim format as P
    with vars
        items = ""
        while .not. tii.fetch_eof()
            items = items + tii.eval("quantity + '|'+product->description+'|'+ price + '|' + alltrim( str((quantity*price),12,2,'$') )")+ crlf()
        end while
        Format.even_row_color="Blue White"
        Format.odd_selected_color="Dark Blue"
        Format.even_selected_color="Dark Blue"
'Apply 'owner draw' formatting information to the list
        items = a5_owner_draw_list_fmt(items,Format)
    end with
end function

The find_invoice displays a popup menu of search alternatives. It then displays an Xdialog that accepts the search parameter. Depending on the user's choice, it sets the order of records using either the <;TBL>.INDEX_PRIMARY_PUT()or <;TBL>.ORDER()methods. It then finds the next matching record using either the <;TBL>.FETCH_GOTO()or <;TBL>.FETCH_FIND()methods. It uses the get_header_variables function to display the results.

function find_invoice as C (vars as P)
    with vars
menu_choices = <;<;%list%
Record Number
Invoice Number
        result = ui_popup_menu(menu_choices)
        text_prompt = alltrim(result) + ":"
        find_what = ""
        if result = "" then
            exit function
        end if
find_result = ui_dlg_box("Find Invoice by " + result,<;<;%dlg%
{text=25,1:text_prompt} [.20find_what] {sp};;
<10&Find!find?.not.(find_what="")> <10&Cancel!cancel>;
        if (find_result = "find") .or. (find_result = "enter") then
            if (find_what <> "") then
                if result = "record number" then
                    tih.fetch_goto( val(find_what) )
                else if result = "invoice number" then
                else if result = "lastname" then
                end if
            end if
        end if
    end with
end function


Desktop applications only.

See Also