Using a Lookup to Set the Current Record

Description

This topic describes how to use a table lookup to set the current record. The example is based on the Customer table of the AlphaSports sample database. The following form shows the Firstname, Lastname, Company, and Phone fields from the Customer table. In addition, there is a drop down combo box with the label "Lookup". The lookup field displays unique field values from the Phone field. When you select a phone number if the lookup field, the other controls on the form update automatically to show fields from the same record.

To create a similar capability:

  1. In the Form Editor click to display the Variable Definition dialog. Create a numeric variable named current and click OK to save your input.

  2. Drag the current variable from the Drag Drop List onto the form.

  3. Right click the new control and select Properties... .

  4. Display the Setup tab.

  5. Change the Field-Type to "Combo Box".

  6. Display the Choices tab.

  7. Change the Choices for control are to "Computed Automatically".

  8. Select "Values from a table/set" in the Source list.

  9. Select "" in the From Table list.

  10. Enter phone + "|" + customer_id in the Field/expression values to display control.

    This expression causes the control to display the phone field but return the value in the customer_id field.
  11. Enter phone in the Display order control.

  12. Check Display only unique keys.

  13. Click OK to return to the Form Editor.

  14. Right click the new control and select Events > OnChange.

  15. Enter the following Xbasic code. This code reads the customer_id value in the current variable, finds the matching record, reads that record, and then refreshes the form to show the new values.

    dim id as C
    dim tbl as P
    tbl = table.current()
    tbl.fetch_goto(current)
    parentform.resynch()
  16. Click 'Save' and return to the Form Editor.

  17. Close the Form Editor.

  18. Display the Tables/Sets tab of the Control Panel.

  19. Select the Customer table and select Design > Indexes.

  20. Create an index for the customer_id field.

  21. Click Build... and Close.

  22. View your form. Now, when you select a phone number in the lookup control, the other fields displayed on the form will be from the same record.

See Also