Conditional Access

No items matching your keywords were found.


Access 2010: Highlight data on forms by using conditional formatting.wmv

Ms Access Tutorial - How to Use Color Coding to Enhance your Applications

Please take a look at this powerful technique to add advanced color coding logic to your forms.

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 And EMR

xcode(x) = 2

Else

xcode(x) = 3

End If

x = x + 1 'Step 5

If IsNull(aggdate) Then

xcode(x) = 0

ElseIf aggdate = Now() And aggdate

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 And aggamnt

xcode(x) = 2

Else

xcode(x) = 1

End If

GoTo aggout

ElseIf WorkDesc = "maj" Then

If aggamnt = 3 And aggamnt

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() And wcompdate 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

About the Author

Jon Watson is the founder of Biomation Systems, Inc. With 26 years experience helping Fortune 500 companies with process improvement he formed BioMation to bring the same expertise to smaller companies that need the same improvements at an affordable price.
You can find more help for MS Access at:
www.biomationsystems.com
www.accessdatabasehelp.com
www.accesshelpebook.com

conditional SQL statement?

I'm making a database to keep a track of my dvds in access. I've got a working SQL query which looks up film names and returns the details of any dvds it finds, the query is linked to a form which displays the returned data, the code looks like this:

Select * from films
where flims.filmname like [forms]![searchform]![filmtitle];

The problem I'm having is that I also want to be optionally able to search by genre as well. As I want to make genre searching optional I cant just use an and clause, because the genre details box might be left empty in which case the query returns no rows. Is there any way to include an If statement in an SQL query, or another way to only include fields into which the user has entered data in the query?

You can conditionally execute one of 3 different queries, depending on user selection from a drop-down list, as one suggestion:

0 selections: execute query with no criteria by genre
1 selection: execute query with genre =
2+ selections: execute query with genre IN