Microsoft KB Archive/304104

= How to programmatically apply conditional formatting settings to all form controls =

Article ID: 304104

Article Last Modified on 4/10/2007

-

APPLIES TO


 * Microsoft Office Access 2007
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q304104



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
In Microsoft Access 2000, conditional formatting was added to forms and reports to allow users to format a control based on a particular condition. However, in the Access user interface, you cannot automatically propagate the conditional formatting of one control to all other controls on the form. But you can do this by using the FormatConditions collection of the control on which the initial conditional format has been set. This article shows you how to use code to propagate the conditional formatting of one control to all other controls on the form.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. For an example of how to propagate the conditional formatting of one control to all other controls on the form, follow these steps.

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

 If you use Access 2002 or Access 2003, follow these steps:  Start Access. On the Help menu, point to Sample Databases, and then click Northwind Sample Database. In the Database window, click the Modules under Objects. Click New to create a new module.  Add the following line to the Declarations section of the module if it is not already there: Option Explicit </li>  Type or paste the following code: Function AddFormats(ctlSource As Control, frm As Form) As Integer

Dim ctl As Control Dim fcdSource As FormatCondition Dim fcdDestination As FormatCondition Dim varOperator As Variant Dim varType As Variant Dim varExpression1 As Variant Dim varExpression2 As Variant Dim intConditionCount As Integer Dim intCount As Integer intConditionCount = ctlSource.FormatConditions.Count For Each ctl In frm.Controls If ctl.Name = ctlSource.Name Then ' This is the source. Don't apply formatting. ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then intCount = 0 ' Bulk remove all current FormatConditions ctl.FormatConditions.Delete Do Until intCount = intConditionCount Set fcdSource = ctlSource.FormatConditions.Item(intCount) varOperator = fcdSource.Operator varType = fcdSource.Type varExpression1 = fcdSource.Expression1 varExpression2 = fcdSource.Expression2 ' Add the FormatCondition ctl.FormatConditions.Add varType, varOperator, varExpression1, varExpression2 ' Reference the FormatCondition to apply formatting. ' Note: The FormatCondition cannot be referenced ' in this manner until it exists. Set fcdDestination = ctl.FormatConditions.Item(intCount) With fcdDestination .BackColor = fcdSource.BackColor .FontBold = fcdSource.FontBold .FontItalic = fcdSource.FontItalic .FontUnderline = fcdSource.FontUnderline .ForeColor = fcdSource.ForeColor End With ' Move to the next FormatCondition intCount = intCount + 1 Loop End If   Next ctl ' Cleanup AddFormats = intConditionCount MsgBox &quot;There were &quot; & AddFormats & &quot; Conditional Format(s) applied to all text and combo boxes except the source.&quot; Set ctl = Nothing Set fcdSource = Nothing Set fcdDestination = Nothing Set varOperator = Nothing Set varType = Nothing Set varExpression1 = Nothing Set varExpression2 = Nothing intConditionCount = 0 intCount = 0

End Function </li> Save the module as Module1 .</li> Open the Orders form in Design view.</li> Click the Freight text box.</li> On the Format menu, click Conditional Formatting, and then apply the following conditional formats to the Freight text box: <ul> Condition 1 = Field Has Focus, Fill/Back Color = Yellow</li> Condition 2 = Field Value Is Equal To 1, Font/Fore Color = Green</li> Condition 3 = Expression Is 1+1=2, Underline = True</li></ul>

</li> Add a command button named cmdApplyCondFormat to the Orders form.</li>  Set the OnClick property of the command button to the following event procedure: Private Sub cmdApplyCondFormat_Click AddFormats Me.Freight, Me End Sub </li> Save the Orders form.</li> Open the Orders form in Form View. Note the conditional formatting of the Freight text box.</li> Click the command button to apply the Freight text box conditional formatting to all other text and combo boxes on the main form.</li></ol> </li> If you use Access 2007, follow these steps: <ol> Start Access, and then open sample datebase.</li> On the Create tab, click Macro in the Other group, and then click Module.</li> <li> Add the following line to the Declarations section of the module if it is not already there: Option Explicit </li> <li> Type or paste the following code: Function AddFormats(ctlSource As Control, frm As Form) As Integer

Dim ctl As Control Dim fcdSource As FormatCondition Dim fcdDestination As FormatCondition Dim varOperator As Variant Dim varType As Variant Dim varExpression1 As Variant Dim varExpression2 As Variant Dim intConditionCount As Integer Dim intCount As Integer intConditionCount = ctlSource.FormatConditions.Count For Each ctl In frm.Controls If ctl.Name = ctlSource.Name Then ' This is the source. Don't apply formatting. ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then intCount = 0 ' Bulk remove all current FormatConditions ctl.FormatConditions.Delete Do Until intCount = intConditionCount Set fcdSource = ctlSource.FormatConditions.Item(intCount) varOperator = fcdSource.Operator varType = fcdSource.Type varExpression1 = fcdSource.Expression1 varExpression2 = fcdSource.Expression2 ' Add the FormatCondition ctl.FormatConditions.Add varType, varOperator, varExpression1, varExpression2 ' Reference the FormatCondition to apply formatting. ' Note: The FormatCondition cannot be referenced ' in this manner until it exists. Set fcdDestination = ctl.FormatConditions.Item(intCount) With fcdDestination .BackColor = fcdSource.BackColor .FontBold = fcdSource.FontBold .FontItalic = fcdSource.FontItalic .FontUnderline = fcdSource.FontUnderline .ForeColor = fcdSource.ForeColor End With ' Move to the next FormatCondition intCount = intCount + 1 Loop End If   Next ctl ' Cleanup AddFormats = intConditionCount MsgBox &quot;There were &quot; & AddFormats & &quot; Conditional Format(s) applied to all text and combo boxes except the source.&quot; Set ctl = Nothing Set fcdSource = Nothing Set fcdDestination = Nothing Set varOperator = Nothing Set varType = Nothing Set varExpression1 = Nothing Set varExpression2 = Nothing intConditionCount = 0 intCount = 0

End Function </li> <li>Save the module as Module1 .</li> <li>Open the Orders Details form in Design view.</li> <li>Click the Customer ID dropdown list, click Conditional, and then apply the following conditional formats to the Customer ID dropdown list: <ul> <li>Condition 1 = Field Has Focus, Fill/Back Color = Yellow</li> <li>Condition 2 = Field Value Is Equal To 1, Font/Fore Color = Green</li> <li>Condition 3 = Expression Is 1+1=2, Underline = True</li></ul> </li> <li>Add a command button named cmdApplyCondFormat to the Order Details form.</li> <li> Set the OnClick property of the command button to the following event procedure: Private Sub cmdApplyCondFormat_Click AddFormats Me.Customer_ID, Me End Sub </li> <li>Save the Order Details form.</li> <li>Open the Order Details form in Form View. Note the conditional formatting of the Customer ID text box.</li> <li>Click the command button to apply the Payment Date text box conditional formatting to all other text and combo boxes on the main form.</li></ol> </li></ul>

Additional query words: inf conditional formatting ACC2002 ACC2003 ACC2007 kbnoOfficeAlertID

Keywords: kbprogramming kbvba kbinfo kbhowto KB304104

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.