MS Access Conditional Formatting

69

By msaccessmemento

Public Function HighLightForeignKeys(argFieldName As String, _

ArgFieldValue As Integer)

Dim FormatCondition As String

Dim CodeReception As Integer

CodeReception = ArgCodeReception

'FormatCondition = "[Code_reception] = " & CodeReception

FormatCondition = "[" & argFieldName & "] = " & ArgFieldValue

With Me.ID

.FormatConditions.Delete

.FormatConditions.Add acExpression, , FormatCondition

.FormatConditions(0).BackColor = 16510422

AddFormats Me.ID, Me

End With

End Function

Add various colored text and other formatting to forms and reports in your database, depending upon which conditions are met, even without the conditional formatting feature provided by Microsoft Access 2000 and later versions.

Microsoft Access 2000 and later versions provide conditional formatting for text boxes and combo boxes that allow you to change the display of the text from the regular settings applied in the Properties window, depending upon whether certain conditions you have identified have been met. This may be colored text, highlighted text, or bold, italic or underlined text. Conditional formatting can even allow you to enable or disable the text box or combo box control. However, this feature isn't available in earlier versions, and there are a few limitations in the built-in conditional formatting, as we'll soon find out.

Microsoft Article

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

loadTOCNode(1, '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

loadTOCNode(1, 'moreinformation');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:1.Start Access.2.On the Help menu, point to Sample Databases, and then click Northwind Sample Database.3.In the Database window, click the Modules under Objects.4.Click New to create a new module.5.Add the following line to the Declarations section of the module if it is not already there:Option Explicit

6.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 "There were " & AddFormats & " Conditional Format(s) applied to all text and combo boxes except the source."

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

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

11.Add a command button named cmdApplyCondFormat to the Orders form.12.Set the OnClick property of the command button to the following event procedure:Private Sub cmdApplyCondFormat_Click()

AddFormats Me.Freight, Me

End Sub13.Save the Orders form.14.Open the Orders form in Form View. Note the conditional formatting of the Freight text box.15.Click the command button to apply the Freight text box conditional formatting to all other text and combo boxes on the main form.•If you use Access 2007, follow these steps:1.Start Access, and then open sample datebase.2.On the Create tab, click Macro in the Other group, and then click Module.3.Add the following line to the Declarations section of the module if it is not already there:Option Explicit

4.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 "There were " & AddFormats & " Conditional Format(s) applied to all text and combo boxes except the source."

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

5.Save the module as Module1.6.Open the Orders Details form in Design view.7.Click the Customer ID dropdown list, click Conditional, and then apply the following conditional formats to the Customer ID dropdown list:•Condition 1 = Field Has Focus, Fill/Back Color = Yellow•Condition 2 = Field Value Is Equal To 1, Font/Fore Color = Green•Condition 3 = Expression Is 1+1=2, Underline = True8.Add a command button named cmdApplyCondFormat to the Order Details form.9.Set the OnClick property of the command button to the following event procedure:Private Sub cmdApplyCondFormat_Click()

AddFormats Me.Customer_ID, Me

End Sub10.Save the Order Details form.11.Open the Order Details form in Form View. Note the conditional formatting of the Customer ID text box.12.Click the command button to apply the Payment Date text box conditional formatting to all other text and combo boxes on the main form.

Comments

FunFacter profile image

FunFacter 3 years ago

A good hub, Can you guide me to filter a report so that only it is opened for a sepecific report. My database is for a gold shop and i have a report for invoices from a query i only want to show the invoice for one customer. ANY WAY??

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    • No HTML is allowed in comments, but URLs will be hyperlinked
    • Comments are not for promoting your Hubs or other sites

    Please wait working