Apply or change conditional formatting on a control

ShowOn a form or report

  1. Open the form in Design view (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.) or Form view (Form view: A view that displays a form to show or accept data. Form view is the primary means of adding and modifying data in tables. You can also change the design of a form in this view.), or the report in Design view.
  2. Do one of the following:
  3. On the Format menu, click Conditional Formatting.
  4. Do one or more of the following:

ShowChange the format of a control based on a value or expression

  1. Do one of the following:

Base conditional formatting on the value of the control    

The control's appearance changes if the value in the underlying field for that record meets criteria that you specify. For example, you can specify the UnitPrice field to be highlighted with a different color when it contains a value between $25.00 and $50.00. Also, you can specify an additional criteria to highlight the UnitPrice field with another color if it contains a value greater than $50.00.

To use values in the selected control as the formatting criteria, under Condition 1, click Field Value Is in the first box, click the comparison phrase in the second box, and then type a value in the third (and fourth) box. You can enter a constant value or an expression.

Base conditional formatting on an expression     

You can also change the appearance of a control when an expression or a Visual Basic® for Applications (VBA) function returns a value of TRUE. For example, you might want the appearance of the ProductName field to match the appearance of the UnitPrice field. To change the format of the ProductName field to match the format of the UnitPrice field, use expressions in the conditional formatting criteria for the ProductName field that check the value of the UnitPrice field.

To use an expression as the formatting criteria (to evaluate data or a condition other than the values in selected controls), click Expression Is in the first box, and then enter the expression in the second box. The expression must return a logical value of TRUE or FALSE.

  1. Select the font style, color, or other formatting that you want to apply. Microsoft Access applies the selected formatting only if the control value meets the condition, or the expression returns a value of TRUE.

ShowTip

You can use conditional formatting to prevent users from making changes to a control, when a condition is true. Specify the criterion, and in the format section, click the Enabled button (the rightmost button in the Conditional Formatting dialog box) once.

  1. To add another condition, click Add, and then repeat steps 1 and 2.

ShowChange the format of the control that has focus in a form

You can make data entry easier by using conditional formatting to highlight the control that has the focus. When the control you're located in is highlighted, it's easier to work with a form that contains many controls, or with a continuous form or a datasheet form that contains many records.

  1. In the Condition x area of the Conditional Formatting dialog box, click Field Has Focus in the first box.
  2. Select the font style, color, or other formatting that you want the field to have when it has focus.

 Note   The procedure described above will highlight the specific control when it gets focus. When focus changes, the control that gets the focus will not appear highlighted unless you repeat the above two steps for that control. To apply conditional formatting to multiple controls, select them before you click Conditional Formatting on the Format menu, and then specify the criteria and format.

ShowChange, add, or remove a condition

  • To change an existing condition, change the criterion, or the current format settings.
  • To add a new condition, click Add.
  • To remove one or more conditions, click Delete, and then select the check box for the condition or conditions you want to delete.
  1. Click OK.

 Notes 

ShowOn a data access page

In a data access page, you can apply conditional formatting to a control programmatically. You can change the format of data based on one or more criteria, or change the formatting of the control that has focus.

The following illustration shows a data access page, where a record is displayed in red if the # In Stock field has a value that is less than or equal to 20.

Data access page with conditional formatting

  1. Open a data access page in Design view.
  2. Do one of the following:
    • On an ungrouped page where the size of the data page is set to 1, add code to the Current event to apply conditional formatting to the controls on the page.
    • On all other pages, add code to the DataPageComplete event to apply conditional formatting to the controls on the page.

ShowSample DataPageComplete event procedure

This event procedure sets the Color property of the controls in the header section to red if the UnitsInStock field has a value that is less than or equal to 20.

<SCRIPT language=vbscript event=DataPageComplete(dscei) for=MSODSC>
<!--
' Highlight items that are almost out of stock.
dim sect
dim dscconst
dim bandHTML

' Check that the event fired for the DataPage in the Products GroupLevel.
If (dscei.DataPage.GroupLevel.RecordSource = "Products") Then
    Set dscconst = MSODSC.Constants
    Set sect = dscei.DataPage.FirstSection

    ' Go through the sections of the event's DataPage object.
    Do
        ' Ignore all sections except the header section.
        If (sect.Type = dscconst.sectTypeHeader) Then
            Set bandHTML = sect.HTMLContainer

            ' Change the text to red if there are <= 20 units in stock.
            If (CInt(bandHTML.children("UnitsInStock").innerText) <= 20) Then
                bandHTML.children("UnitsInStock").style.color = "red"
                bandHTML.children("UnitPrice").style.color = "red"
                bandHTML.children("ProductName").style.color = "red"
            End If
        End If
        Set sect = sect.NextSibling
    Loop until (sect is nothing)
End If
-->
</SCRIPT>
 
 
Applies to:
Access 2003