Defining a code table

To reduce storage needs, frequently you might want to store short, encoded values in the database, but these encoded values might not be meaningful to users. To make DataWindow objects easy to use, you can define code tables.

Each row in a code table is a pair of corresponding values: a display value and a data value. The display values are those users see at runtime. The data values are those that are saved in the database.

Limit on size of data value

The data value you specify for the Checkbox, DropDownListBox, Edit, EditMask, and RadioButtons edit styles is limited to 255 characters.

How code tables are implemented

You can define a code table as a property of the following column edit styles:

  • Edit

  • DropDownListBox

  • RadioButtons

  • DropDownDataWindow

  • EditMask, using spin control

The steps to specify the code table property for each edit style are similar: you begin by defining a new edit style in the Database painter. Once you select an edit style, use the specific procedure that follows to define the code table property.

For how to create an edit style, see About edit styles.

Allowing null values

An internal PowerBuilder code, NULL!, indicates null values are allowed. To use this code, specify NULL! as the data value, then specify a display format for nulls for the column.

To define a code table as a property of the Edit edit style

  1. Select the Use Code Table check box.

  2. Enter the display and data values for the code table.

  3. If you want to restrict input in the column to values in the code table, select the Validate check box.

    For more information, see Validating user input.

To define a code table as a property of the DropDownListBox edit style

  1. Enter the display and data values for the code table.

  2. If you want to restrict input in the column to values in the code table, clear the Allow Editing check box.

    For more information, see Validating user input.

To define a code table as a property of the RadioButtons edit style

  • Enter the display and data values for the code table.

To define a code table as a property of the DropDownDataWindow edit style

  1. Specify the column that provides the display values in the Display Column box.

  2. Specify the column that provides the data values in the Data Column box.

  3. If you want to restrict input in the column to values in the code table, clear the Allow Editing check box.

To define a code table as a property of the EditMask edit style

  1. Select the Spin Control check box.

  2. Select the Code Table check box.

  3. Enter the display and data values for the code table.

How code tables are processed

When data is retrieved into a DataWindow object column with a code table, processing begins at the top of the data value column. If the data matches a data value, the corresponding display value displays. If there is no match, the actual value displays.

Consider the example in the following table.

Display values

Data values

Massachusetts

MA

Massachusetts

ma

ma

MA

Mass

MA

Rhode Island

RI

RI

RI


If the data is MA or ma, the corresponding display value (Massachusetts) displays. If the data is Ma, there is no match, so Ma displays.

Case sensitivity

Code table processing is case sensitive.

If the code table is in a DropDownListBox edit style, and if the column has a code table that contains duplicate display values, then each value displays only once. Therefore, if this code table is defined for a column in a DataWindow object that has a DropDownListBox edit style, Massachusetts and Rhode Island display in the ListBox portion of the DropDownListBox.

Validating user input

When users enter data into a column in a DataWindow object, processing begins at the top of the display value column of the associated code table.

If the data matches a display value, the corresponding data value is put in the internal buffer. For each display value, the first data value is used. Using the sample code table, if the user enters Massachusetts, ma, or Mass, the data value is MA.

You can specify that only the values in the code table are acceptable:

  • For a column using the Edit edit style, select the Validate check box.

    If you have requested validation for the Edit edit style, an ItemError event is triggered whenever a user enters a value not in the code table. Otherwise, the entered value is validated using the column's validation rule, if any, and put in the DataWindow buffer.

  • For the DropDownListBox and DropDownDataWindow edit styles, clear the Allow Editing check box: users cannot type a value.

    Although users cannot type a value when Allow Editing is false, they can search for a row in the drop-down list or DataWindow by typing in the initial character for the row display value. The search is case sensitive. For the DropDownDataWindow edit style, the initial character for a search cannot be an asterisk or a question mark. This restriction does not apply to the DropDownListBox edit style.

    When the code table processing is complete, the ItemChanged or ItemError event is triggered.

Code table data

The data values in the code table must pass validation for the column and must have the same datatype as the column.