Lookup Tables

Pick Lists are the primary way for users to enter data which is fairly static and not likely to change. Normally, there are 5 to 20 possible options (such as picking one of serveral pre-selected colors or selecting a state). This method is used to reduce the number of spelling errors (or variations - JSmith, J.Smith, J.L.Smith, John Smith). (For fewer than 8 options, radio buttons are usually prefered, for lots of options, a search engine helps.) This technique is usually implemented using Lookup Tables.

On Forms, this is normally styled as a combo box (an edit box with a down arrow).

There are basically, 3 types of lookup tables

There are 2 ways to store the lookup values The generic structure for a lookup table is
ID Either an automatically generated number, or a short string, perhaps only a single letter.
ShortDescriptor This is what is displayed in the pick list. Probably less than 20 characters.
Description This can be a whole paragraph. It can be displayed in a report's footnotes, or in a pop-up box for the field (right click and select Description).
Qualifier These fields provide a method to select only a subset of the available values.
You should always use a naming convention which explicitly distinguishes those tables who's only purpose is a lookup table.
SomeName_LU LU for LookUp
SomeNameID MS Access uses this. The primary key has the same name.
One variation is to store all the lookup values in a single table (instead of a separate table for each field). This may significantly simplify some documentation and CM issues (such as one report for all lookup values instead of a separate report for each lookup table). Then the qualifiers would include the associated Table and Field names.

Paradox | MS Access | Delphi | Visual Basic | Java


Paradox 5.0

Pick lists are normally defined when the form is designed. There is no easy way to base them on a table.

Here is a generic routine used to populate a pick list (ListUI) from LookUpTC (TCursors (TC) point to a table - opened in another routine).

The following code fragment opens LookupTC and populates 2 pick lists. (In the example, Forms and Reports are referred to as Methods because they perform actions - view, print, format - on the data.) LookUpTC was one of 3 global variables used by the application.


MS Access 97

MS Access allows you to specify the type of pick list when designing the table. Then, when a form is designed, the field is automatically configured according to the specification attached to the table. However, you can still change the specifics on a per form basis.

The help is kind of sketchy - and it always points you to using the wizard (available on the form and in table design).
Value List You can type in a fixed list of allowed values, separated by semicolons.
red;blue;green
Table/Query This can be used to get all the values in a table. A query will get selected values, it is rerun using ObjectName.requery
Field List No clue, this appears to be a subset of Table/Query

Be sure to set Limit to List according to your needs.

In order to use the form wizard to design a combo box, make sure that the Control Wizard's button is pressed in (on the toolbox pallet, it has a wand icon), and then place a combo box on the form.
Row Source Type Row Source
Value List "red";"blue";"green"
Table/Query select fielda, fieldb from table;
Table/Query tableName
MS Access provides a On Not in List event for combo boxes. You can use this to add new values to the lookup table.


Dynamically Setting the Contents of a Combo Box

It is possible to dynamically change the options displayed in a Combo Box's picklist based on the value in another component. Basically, the query for the combo box needs a where clause which references the controlling component. Then the query must be re-run each time the down arrow is clicked (via On Got Focus). This example is from a working piece of code. MS Access help never explains how to reference a UI Object in a query. Based on trial and error, the example above seems to work.

Using On Got Focus allows Text4 to be attached to a table. Then, as you use the Forward and Back buttons to step through the table, the contents of the pick list are updated each time the list is opened. The form's On Current event also works, but it re-runs the query for each record displayed, not each time the list is displayed.

I actually had a form where none of the data would display if the Requery was in the comboBox's On Got Focus event and the comboBox was attached to a field in the table. Moving it to the form's On Current event solved the problem. This appears to be related to the setting of the Column Widths property - if the first field has a display width of zero (typical with lookup tables), then the Requery must be in the form's On Current event. Unfortunately, in datasheet view, none of the data will display until you click on the field.

MS Access provides an example of using one combo box to control the contents of another combo box in

In this example,


Related Links

Additional information is provided at


Delphi 5.0

Delphi provides several different types of comboboxes. The only obvious way to force the entered value to be from the list, is to set the style property to csDropDownList. There is no event to trap values that are not in the list.


Dynamically Setting the Contents of a Combo Box

Simply change the associated dataset's filter to dynamically change the options displayed in a Combo Box's picklist based on the value in another component. The following is from the Setting the Filter property help. You may need to call RefreshLookupList.


Visual Basic 6.0


Java


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Databases / LookupTables.html