XML RSS
What is this?
Add to My Yahoo!
Add to My MSN
Add to Google

Home
Whats New!
Need a database?
Tips via email
Learn VBA
Questions Answered
Access Tips Index


Conditional Formatting Advanced

The Conditional Formatting Introduction tip introduced you to ‘Conditional Formatting’ in Microsoft Access. This week we will be looking at using the tool to achieve some visual effects on your form.

If you wish to download a database to view this week’s lesson, then paste the following URL into your web browsers address line:

http://h1.ripway.com/jmisson/Newsletter%20Database/148Database.mdb

I was recently asked to set up a database where the user could visually see where a particular process was up to. There were four stages. Each stage was assigned to a square, therefore there were four squares. As each stage was completed he wanted the square relating to that stage to change color, he would achieve this by clicking on the square.

If no stages were complete – all four squares were white.

If the first stage was complete, the first square was yellow, with the remaining 3 squares white.

If the second stage was complete, the first square remained yellow and the second square changed to orange, with the last two squares remaining white.

If the third stage was completed then the first two squares remained yellow and orange respectively and the third square turned blue.

If the fourth stage (therefore all stages of the process) was completed then all the squares turned green. This meant the process was finished and the product was ready.

Are you following so far?

This process needed to be undertaken for each appointment, throughout the day, therefore a continuous form was needed to display all the appointments, thus the need for conditional formatting. If it was just in a single form, then conditional formatting would not be needed, you could achieve the same result by updating the colours by code. This is not possible in a continuous form as not all the records have the focus; therefore it is impossible for formatting to change on all of the records without using conditional formatting.

To set this up:

Create a new table with the following fields:

ID – Autonumber

Time - Date/Time (Short Time)

StoreValue – Number

Add the some times, into the time section, I add all times in half hour intervals from midnight to midnight (you can view this in the database mentioned above)

Build a continuous form based on this table. Include just the time field and the store value field.

Now we want four instances of the ‘StoreValue’ text field, you are most likely to only have one at present. Therefore copy and paste this field three more times so as you have four. Line these up all in a row beside each other. Change the names of each of these boxes to:

SV1

SV2

SV3

SV4

You can do this by right clicking on each of them, selecting ‘Properties’ from the drop down list and clicking on the ‘Other’ tab in the properties window, then next to ‘Name’ enter the above names. This will help keep things consistent.

Change the font to white, so as you cannot see the writing. This makes it look like you have 4 white boxes on each of your records.

The next step is disabling all the boxes except the first one, this is to control the user on which box they can click. Therefore set the following properties for the last three boxes (can be found under the ‘Data’ tab in the properties window)

Enabled = False Locked = True

The next thing we are going to do is to set the conditional formatting for each of the boxes depending on the value of ‘StoreValue’ as each box is linked to ‘StoreValue’ then the value for each of the boxes in each of the records is the same at any one time.

Click on SV1, then on the menu bar, select ‘Format’ then ‘Conditional Formatting’. The conditional formatting window will open.

You will see the default is white on white. In the Conditon1 sections enter the following in the appropriate area:

Field Value Is Between 1 and 3

Choose ‘Yellow’ background and font.

Click on Add, then add the following to the next section:

Field Value Is Equal To 4

Choose ‘Green’ background and font.

Repeat for SV2 with the following scenarios:

Field Value Is Between 2 and 3 (Orange background and font)

Field Value Is Equal To 4 (Green background and font)

SV3

Field Value Is Equal To 3 (Blue background and font)

Field Value Is Equal To 4 (Green background and font)

SV4

Field Value Is Equal To 4 (Green background and font)

Once you have all that set up, we now need to add some coding to change the value of ‘StoreValue’ this will in turn change the colors of the squares.

If you have not coded before do not worry as I will step you through it.

Select the first square SV1, open the properties window for this textbox and click on the ‘Event’ tab. Next to ‘On Click’ click on the ‘…’ button. You will need to place your cursor next to ‘On Click’ for this button to appear.

Select ‘Event Procedure’ if prompted.

The VBA coding window will open with the following two lines of code:

Private Sub SV1_Click()

End Sub

In between these two lines of code type the following:

If Me!SV1 = 0 Then

Me!SV1 = 1

Me!SV1.Enabled = True

Me!SV1.Locked = False

Me!SV2.Enabled = True

Me!SV2.Locked = False

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = False

Me!SV4.Locked = True

Else

If Me!SV1 = 1 Then

Me!SV1 = 0

Me!SV1.Enabled = True

Me!SV1.Locked = False

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = False

Me!SV4.Locked = True

End If

End If

Close the VBE window by clicking on the outer X.

Repeat for SV2, but this time add the following code to the ‘On Click’ event:

If Me!SV1 = 1 Then

Me!SV1 = 2

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = True

Me!SV2.Locked = False

Me!SV3.Enabled = True

Me!SV3.Locked = False

Me!SV4.Enabled = False

Me!SV4.Locked = True

Else

If Me!SV1 = 2 Then

Me!SV1 = 1

Me!SV1.Enabled = True

Me!SV1.Locked = False

Me!SV2.Enabled = True

Me!SV2.Locked = False

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = False

Me!SV4.Locked = True

End If

End If

Repeat again for SV3, but add the following code:

If Me!SV1 = 2 Then

Me!SV1 = 3

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = True

Me!SV3.Locked = False

Me!SV4.Enabled = True

Me!SV4.Locked = False

Else

If Me!SV1 = 3 Then

Me!SV1 = 2

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = True

Me!SV2.Locked = False

Me!SV3.Enabled = True

Me!SV3.Locked = False

Me!SV4.Enabled = False

Me!SV4.Locked = True

End If

End If

Finally repeat for SV4, adding the following code:

If Me!SV1 = 3 Then

Me!SV1 = 4

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = True

Me!SV4.Locked = False

Else

If Me!SV1 = 4 Then

Me!SV1 = 3

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = True

Me!SV3.Locked = False

Me!SV4.Enabled = True

Me!SV4.Locked = False

End If

End If

We have one final bit of code to add, this is to make sure you can click the appropriate square as the focus moves from record to record. This time we are going to add some code to the ‘On Current’ event for the form, this event is triggered when the focus of the record changes. To get to this event, right click in upper left hand corner of the form (where the two rulers meet) and select ‘properties’ from the list provided. Click on the ‘Event’ tab. Place your cursor next to ‘On Current’ and click on the ‘…’ button.

When the VBE window opens, add this code between the two lines of code already created:

Select Case Me!SV1

Case 0

Me!SV1.Enabled = True

Me!SV1.Locked = False

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = False

Me!SV4.Locked = True

Case 1

Me!SV1 = 1

Me!SV1.Enabled = True

Me!SV1.Locked = False

Me!SV2.Enabled = True

Me!SV2.Locked = False

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = False

Me!SV4.Locked = True

Case 2

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = True

Me!SV2.Locked = False

Me!SV3.Enabled = True

Me!SV3.Locked = False

Me!SV4.Enabled = False

Me!SV4.Locked = True

Case 3

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = True

Me!SV3.Locked = False

Me!SV4.Enabled = True

Me!SV4.Locked = False

Case 4

Me!SV1.Enabled = False

Me!SV1.Locked = True

Me!SV2.Enabled = False

Me!SV2.Locked = True

Me!SV3.Enabled = False

Me!SV3.Locked = True

Me!SV4.Enabled = True

Me!SV4.Locked = False

End Select

Close the VBE window

Hope you did not find this too complicated i.e. I hope I was clear.

 

Do you like this tip? Subscribe to my weekly Newsletter to receive tips weekly via email. Click Here to subscribe.

footer for Microsoft Access page