Dynamic List Table Lookups

Description

Many applications call for the contents of list boxes to dynamically change as variables (external to the list box) change.

 Xbasic Example

Alpha Anywhere adds a new approach to populating and refreshing dynamic list boxes and other field bound controls that have a Choices tab in their Properties dialog boxes. The new option is: "Populated using Xbasic".

A button illustrates how to use this option.

  1. Click 'Click Here for Example Code'.

  2. Enter a list of choices, one per line. Use the 'AZ up' and 'AZ' down buttons to sort them.

  3. Click OK to display the Xbasic Code dialog box.

  4. Optionally, click Copy to Clipboard, then paste the code into the OnInit event of your form.

  5. The code can be run from other locations. For example, you could put a button on your form and reload the contents of the list box by redefining <<%str% ... %str% and then setting the new value of pObj.settings.dynamic_list.

 Cascading Dynamic List Using Xbasic

A common application requirement is to dynamically populate a list box based on a selection in another list box. This example is based on the AlphaSports sample database.

The OnInit event of the form contains the following script. This script populates the two list boxes with all unique values available for the selected fields. The pointers are global because we will use them later. Note that each TABLE.EXTERNAL_RECORD_CONTENT_GET() command uses UNIQUE_KEY_VALUE() to retrieve unique values. There is no attempt yet to filter the lists.

dim global pVendor as P
dim global pDescription as P
pVendor = topparent:VendorList.this
pVendor.settings.dynamic_list = table.external_record_content_get("vendor", "Name", "Name", "unique_key_value()")
pDescription = topparent:DescriptionList.this
pDescription.settings.dynamic_list = table.external_record_content_get("product", "Description", "Description", "unique_key_value()")

The Vendor->Vendor_ID field links to the Product->Vendor field. Since the Vendor list box provides the Vendor->Name field, we need an intermediate query to retrieve current record's Vendor->Vendor_ID field. The following script is in the onchange event of the Vendor list box.

dim ven as C
dim vent as C
vent = alltrim(VendorList.text)
ven = table.external_record_content_get("Vendor", "Vendor_ID", "", "alltrim(Name) = " + s_quote(vent))

Once we have the current record's Vendor->Vendor_ID field, we use it to retrieve a list of matching Product->Description values.

pDescription.settings.dynamic_list = table.external_record_content_get("product", "Description", "Description", "alltrim(Vendor) = " + s_quote(ven))
topparent.resynch()

Finally, we refresh the form to display the new values in the Product list box.

topparent.resynch()

 Xdialog Example

Here is a sample Xdialog list box that responds to two shared variables and repopulates when you click a button. The following are two before and after pictures that show the list in action.

dim shared cid_start as C
dim shared cid_end as C
dim SHARED cust as C
ok_button_label = "&OK"
cid_start="A"
cid_end = "L"
cmd = "kl=customer,{keylist_build(\"H=.025,1:25,2:25,3:10\",Customer_id,Lastname,Firstname,Customer_id)}{between(Lastname,"+quote(cid_start)+","+quote(cid_end)+")}"
ui_modeless_dlg_box("Dynamic List Box",<<%dlg%
{can_exit=ok}
{region}
Select customer between:| [.10cid_start!changed] [.10cid_end!changed]
{endregion};
{region}
Select customer:;
[%@cmd%.80,10cust];
{endregion};
{line=1,0};
{region}
<*15=ok_button_label!OK>
{endregion};
%dlg%,<<%code%
if a_dlg_button="changed" then
    a_dlg_button=""
    cmd = "kl=customer,{keylist_build(\"H=.025,1:25,2:25,3:10\",Customer_id,Lastname,Firstname,Customer_id)}{between(Lastname,"+quote(cid_start)+","+quote(cid_end)+")}"
    ui_modeless_dlg_refresh("Dynamic List Box")
end if
if a_dlg_button="OK" then
    ui_modeless_dlg_close("Dynamic List Box")
end if
%code%)

 Action Script Example

This example is based on the AlphaSports sample database.

  1. Create a new action script and select "Dialog Boxes" and "Display an Xdialog Box".

  2. Enter Variable Name "vendor_id", Prompt "Vendor Records", Width "70", and Height "10".

  3. For the Style select "Record List-List Box" and click Define Record List.

  4. From the Table list select "vendor".

  5. Click Add Column to add "Vendor_id" and "Name" to the Columns in Record List field. Their widths are 58 characters and 58 characters.

  6. Select "Vendor_id" for the Return Value field. This is the linking field between the Vendor and Product tables.

  7. Click OK.

  8. Click Add New Variable.

  9. Enter Variable Name "prod_id", Prompt "Product Records", Width "70", and Height "10".

  10. For the Style select "Record List-List Box" and click Define Record List.

  11. From the Table list select "product".

  12. Click the Filter icon to display the Filter Builder.

  13. Select "Vendor" in the left-most list.

  14. Select "is equal to" in the middle list.

  15. Click the Value icon to display the Value dialog box.

  16. Click the Select Variable button to select the "vendor_id" field.

  17. Select "Action Script" and "vendor_id".

  18. Click Insert.

  19. Click OK to return to the Filter Builder.

  20. Click Add Column to add "Product_id", "Vendor", and "Description" to the Columns in Record List field. Their widths are 12 characters, 12 characters, and 46 characters.

  21. If you were going to do something with the product selection, you would presumably select "Product_id" in the Return Value field

  22. Click OK and Next >.

  23. Optionally, enter values for the Dialog Title, Header Text, and Footer Text.

  24. Click Next >.

  25. Optionally, modify the comment that describes the action script.

  26. Click Finish. The resulting dialog box appears below.

See Also