MS Access Tutorial - How to use Color Coding to enhance your applications
Color coding adds value to a form as it easily directs the user's eye to critical data. This is a powerful technique that allows a user to quickly evaluate a pages of data for errors or warning information.
Access 2000 to Access 2003 has a limit of four colors you can use for each field on a continuous form. Using the condition code methos described in this tip, there is no limit to the logic you can apply to set the field conditions to one of the four color options.
Create a table name it ColorCoding
Enter the following fields (without the numbers)
1] CntrID as Autonumber, make it the primary key field
2] Cntname as text
3] WorkDesc as text - the data should either be "min" or " maj"
4] EMR as number, single - the data should be any integer between 1 and 10
5] aggdate as date/time
6] aggamnt as currency
7] wcompdate as date/time
8] wcompamnt as currency
9] concode as number, long integer this field will be calculated so leave it blank.
Enter some data in the table for testing.
Create a form continuous style form using the ColorCoding table as the data source.
Add a form header/footer.
Put a form title in the form header section of the form (optional).
Use the other tab on the properties form and give each field the same name as it's control source. So, name the [EMR] field "EMR" for example. Then you Can use the field names like variable names when you create VBA code.
Add all the fields to the table and align the in one row with the field labels above each field but in the form header sectionof the form.
This should leave one row of fields in the detail section of the form.
Put a button in the footer with the caption refresh.
In design mode on your form, choose Format / Conditional Formating.
Use the Mid command to check a character in the condition code and set your condition colors. Look at the [EMR] field. It is the fourth field from the left. The Mid command directs Access to check the field concode in the fourth chracter place for a span of 1 character for the value of 3.
Set the conditions like this
Leave the default formatting set to white background and black font color.
Expression is Mid([concode],4,1)="3" Pick a color -- I use Red for 3 or an failing condition
Expression is Mid([concode],4,1)="2" Pick a color-- I use yellow for 2 or a warning condition
Expression is Mid([concode],4,1)="1" Pick a color -- I use green for 1 or a passing condition
Repeat this for each field you wish to set conditions on. Note that each character of the condition code starting from left to right will represent a respective field from left to right. So, a 3 in the 2 place on the condition code field will set the CntName field to red. See the field numbers above. Subsequent fields follow the same sequence.
One more example for the [aggdate] field the condtional format would look like:
Expression is Mid([concode],5,1)="3" Pick a color. This
The condition code field, [concode], values are created with any logic that you want to assign to it. In this example I am using eight fields so the conidition code is eight characters wide and each character could be either a 0,1,2, or 3. So it may look like this 10112321. Now using the mid command above, the fourth character is a 1 so the condition for the fourth field,[ EMR], would be a 1 which conditions to green.
Once you have the form designed and tested, you may want to go back and set the concode field to invisible. Your users don't need to see that data.
After you have created the form apply the following code. You can cut and paste this right into the form if you used the same field names described without any typing.
Cut here --------
Option Compare Database
Dim rec As DAO.Recordset
Dim db As Database
Public Function SetConCode()
On Error GoTo erout
Dim xcode(10) As Integer, x As Integer, CCode As String, y As Integer
' xcode values 1 = green, 2 = yellow, 3 = red, 0 =white
' Use the next line if you have hundreds of records and don't want to wait on the screen to update
'DoCmd.Echo False
'Set the field counter to Zero
x = 0
'increment the field counter as you go
x = x + 1 'Step 1
If IsNull(CntrID) Then
xcode(x) = 0
Else
xcode(x) = 1
End If
x = x + 1 'Step 2
If IsNull(Cntname) Then
xcode(x) = 0
Else
xcode(x) = 1
End If
x = x + 1 'Step 3
If IsNull(WorkDesc) Then
xcode(x) = 0
Else
xcode(x) = 1
End If
x = x + 1 'Step 4
If IsNull(EMR) Then
xcode(x) = 0
ElseIf EMR <= 3 Then
xcode(x) = 1
ElseIf EMR > 3 And EMR <= 7 Then
xcode(x) = 2
Else
xcode(x) = 3
End If
x = x + 1 'Step 5
If IsNull(aggdate) Then
xcode(x) = 0
ElseIf aggdate <= Now() Then
xcode(x) = 3
' check to see if the date fails between today and 10 days from now.
ElseIf aggdate >= Now() And aggdate <= Now() + 10 Then
xcode(x) = 2
Else
xcode(x) = 1
End If
x = x + 1 'Step 6
' You can use any amount of logic you need to set the condition you want and then drive
' the conditional formatting to the color you desire.
' In this example I use two sets of condition values depending on the value of another field alltogether.
If IsNull(aggamnt) Then
xcode(x) = 0
GoTo aggout
End If
If WorkDesc = "min" Then
If aggamnt < 1 Then
xcode(x) = 3
ElseIf aggamnt >= 1 And aggamnt <= 5 Then
xcode(x) = 2
Else
xcode(x) = 1
End If
GoTo aggout
ElseIf WorkDesc = "maj" Then
If aggamnt < 3 Then
xcode(x) = 3
ElseIf aggamnt >= 3 And aggamnt <= 7 Then
xcode(x) = 2
Else
xcode(x) = 1
End If
End If
aggout:
x = x + 1 'Step 7 wcompdate
If IsNull(wcompdate) Then
xcode(x) = 0
ElseIf wcompdate <= Now() Then
xcode(x) = 3
ElseIf wcompdate >= Now() And wcompdate <= Now() + 10 Then
xcode(x) = 2
Else
xcode(x) = 1
End If
x = x + 1 'Step 8 wcompamnt
If IsNull(wcompamnt) Then
xcode(x) = 0
ElseIf wcompamnt <= 3 Then
xcode(x) = 3
ElseIf wcompamnt > 3 And wcompamnt <= 7 Then
xcode(x) = 2
Else
xcode(x) = 1
End If
'set the Cntname , field 2, color coding based on a 2 or 3 showing
'any where in the concode field after the 4th character
If InStr(4, concode, "2") > 1 Then
xcode(2) = 2
End If
If InStr(4, concode, "3") > 1 Then
xcode(2) = 3
End If
'Build conCode field values from xcode values collecting the value of X from left to right
'
y = 0
For y = 1 To 8
If y = 1 Then
CCode = xcode(y)
Else
CCode = CCode & xcode(y)
End If
Next
'MsgBox "setconcode finished"
concode = CCode
erout:
Debug.Print Err.Description
DoCmd.Echo True
End Function
'Run the SetConCode function after each field is updated
Private Sub addamnt_AfterUpdate()
SetConCode
End Sub
Private Sub aggdate_AfterUpdate()
SetConCode
End Sub
Private Sub Cntname_AfterUpdate()
SetConCode
End Sub
Private Sub EMR_AfterUpdate()
SetConCode
End Sub
Private Sub Form_Open(Cancel As Integer)
'Loop through all the records setting the concode values when the form is opened to check date values.
Dim x As Integer
x = 1
Set rec = Me.Recordset.Clone
rec.MoveLast
If rec.RecordCount = 0 Then
MsgBox "NO Records Found"
End If
rec.MoveFirst
DoCmd.GoToRecord acDataForm, "CondCodes", acFirst
For x = 1 To rec.RecordCount - 1
If x = rec.RecordCount Then
GoTo getoutloop
Else
SetConCode
DoCmd.GoToRecord acDataForm, "CondCodes", acNext
End If
Next
getoutloop:
rec.Close
End Sub
Private Sub set_Click()
'Loop through all the records setting the concode values
Dim x As Integer
x = 1
Set rec = Me.Recordset.Clone
rec.MoveLast
If rec.RecordCount = 0 Then
MsgBox "NO Records Found"
End If
rec.MoveFirst
DoCmd.GoToRecord acDataForm, "CondCodes", acFirst
For x = 1 To rec.RecordCount - 1
If x = rec.RecordCount Then
GoTo getoutloop
Else
SetConCode
DoCmd.GoToRecord acDataForm, "CondCodes", acNext
End If
Next
'DoCmd.Echo True ' turn echo back on if you turned it off
getoutloop:
rec.Close
End Sub
Private Sub wcompamnt_AfterUpdate()
SetConCode
End Sub
Private Sub wcompdate_AfterUpdate()
SetConCode
End Sub
Cut Here --------------
You can download the example database file from our site at www.biomationsystems.com. Look for the Access Tips link.
This example was created with Access 2002. There may be better ways to accomplish the same thing, but this works well for us. You will need to set the VBA references to include microsoft DAO 3.6
This should do it. You can find the complete example on our web site with a sample database included.
Please note that this is a powerful and valuable tip that can help you set your database applications apart and make your users want more. We work hard to provide these tutorials and would very much appreciate you visiting our site and taking a look at our site and giving us some feedback.
We develop custom applications that solve complex data problems for businesses of all sizes. Our users ask us back because we provide great value. We are looking for partners in process improvement.
If you know of anyone who would benefit from this type of Access help, please feel free to pass this email along.
We have combined two of our favorite utilities into one great limited time special offer. The email tool that we use to keep in touch with our customers and a backup scheduler that allows you to schedule your data to be safely backed up in two different locations.
One Day...
Jon E. Watson
President, BioMation Systems, Inc.
Look for our products and services at www.biomationsystems.com