Conditional Formatting in Continuous Form
Conditional formatting is a magical option to change color (background/foreground) of textbox controls on Forms or Report. The color change is possible when one or more field values meets a specific condition. The specific condition can be tested on the field, where we want to set color on or can be based on values in some other field.
The color change has three different possible choices and their variations:
- Color change when the field has focus.
- Color change when the field value meets a specific condition.
- Color change depending on conditional values in some other field.
We will try out an example, to learn how to change the background/foreground colors of all fields of certain records, which meets a specific condition on a single field value, on a continuous form.
A sample image of a Form with conditional formatting is given below for reference:
The records are formatted based on values in the UnitPrice field. The background color is set on records with UnitPrice value is greater than $30. The font is set with Red color on records with UnitPrice value falls between 20 and 30.
- Import the Order Details and Products Tables from the Northwind.accdb sample database.
- Design a continuous form for the Order Details Table, like the sample image below:
- Open the Order Details Form in Design View.
- Select the OrderID field and open the Conditional Formatting dialog control.
- Select Expression Is under Condition 1 and type [UnitPrice]>30 in the right-side control.
- Select Fill/Back Color to display the color palette and select a light color for the background.
- Click on the Add>> button to display options for a second condition for the same field.
- Select Expression Is in Condition 2 control.
- Type the expression [UnitPrice]>=20 And [UnitPrice]<=30 in the next text control.
- Select Red Font Color for formatting from the color palette and click OK to close the formatting dialog box.
- Repeat Steps-4 to Step-10 for ProductID, Quantity and Discount fields.
- Select UnitPrice field and display the Conditional Formatting dialog control.
- Select Field Value Is in the Condition 1 control.
- Select the same background color selected, for other fields for formatting, from the Color Palettes.
- Click on the Add>> Button to add a new condition for the same field.
- Select Field Value Is in Condition 2 control.
- Select Between from the drop-down list in the next control.
- Type 20 in the next control and type 30 in the last control.
- Select the Red Font Color for formatting from the color palettes and click OK to close the conditional formatting dialog box.
- Save the Form and open it in normal view.
You should see the form view similar to the sample image shown above. Scroll the form down to view more records with conditional formatting.