Changing Font Color Conditional Formatting
With conditional formatting feature of Microsoft Access we can apply up to three colors to the font or background of a Textbox, because only three set of conditions can be set on a field at one time. If we need more than that then what? Well, we can do that job ourselves by checking for the specific condition and apply whatever color we want to the font.
For example, we have a Category Code Combo Box Field on the Form having values range from A to F. When the user selects one of this code from the Combo box the item Description Field’s Font Color should change as per the following color table:
Let us try this out on a sample Form.
- Open a new Form in Design View.
- Click on the Control Wizards button on the Toolbox to enable it.
- Select the Combo box Tool and draw a Combo box in the Detail Section of the Form.
- Select the Radio Button with the Caption: I will Type in the Values that I want then Click Next.
- Type A, B, C, D, E & F in separate rows under Col1 and Click on Finish Command Button.
- While the Combo box is still in selected state display the Property Sheet (F4).
- Click on the Other Tab of the Property Sheet.
- Change the Name Property Value to cboCat.
- Select the Event Tab on the Property Sheet.
- Select [Event Procedure] from the After Update Event Property.
- Click on the Build (. . .) button at the right edge of the After Update Event property to open the VBA Module of the Form.
- Copy and paste the following Code overwriting the empty lines of the After Update Event Procedure:
Private Sub cboCat_AfterUpdate() Dim strtxt, num As Integer Dim colr As Long strtxt = Nz(Me![cboCat], "") If Len(strtxt) > 0 Then num = Asc(strtxt) - 64 colr = Choose(num, 32768, 10485760, 16711680, 65280, 128, 255) With Me!Desc .ForeColor = colr End With End If End Sub
- Close the VBA Editing Window to come back to the Form Design.
- Create a Textbox to the right of the Combobox.
- Display it’s Property Sheet (F4) and select the Other Tab.
- Change the Name Property Value to Desc.
- Save and Close the Form with the name Sample.
- Open the Sample Form in Normal View.
- Type your name in the Text box.
- Try out our creation by selecting the Category Code (A,B,C,D,E,F) one after the other or in random order and watch the color of your name changes.
Microsoft Excel Power in MS-Access