vba conditional formatting

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Access VBA Training and help » VBA conditional formatting

VBA conditional formatting

resolvedResolved · Urgent Priority · Version 2003

David has attended:
Access VBA course

VBA conditional formatting

Hi. I've put the code below in to my form and the ID field formats red, green and yellow fine, however, the time_limit field is purely red. Is it not possible to use dynamic conditional formatting in vba or is there something wrong with my code? Thanks. David.

Private Sub Form_Load()

Dim objFrc As FormatCondition

'delete existing formats
Me.ID.FormatConditions.Delete
Me.TIME_LIMIT.FormatConditions.Delete

'create three format objects and add them to the FormatConditions collection
Set objFrc = Me!ID.FormatConditions.Add(acFieldValue, acLessThan, 4)
Set objFrc = Me!ID.FormatConditions.Add(acFieldValue, acEqual, 4)
Set objFrc = Me!ID.FormatConditions.Add(acFieldValue, acGreaterThan, 4)

'create three format objects and add them to the FormatConditions collection
Set objFrc = Me!TIME_LIMIT.FormatConditions.Add(acFieldValue, acLessThan, DATE)
Set objFrc = Me!TIME_LIMIT.FormatConditions.Add(acFieldValue, acEqual, DATE)
Set objFrc = Me!TIME_LIMIT.FormatConditions.Add(acFieldValue, acGreaterThan, DATE)

'format the time_limit text box with each condition
With Me.ID.FormatConditions(0)
.BackColor = vbGreen
End With

With Me.ID.FormatConditions(1)
.BackColor = vbYellow
End With

With Me.ID.FormatConditions(2)
.BackColor = vbRed
End With

With Me.TIME_LIMIT.FormatConditions(0)
.BackColor = vbGreen
End With

With Me.TIME_LIMIT.FormatConditions(1)
.BackColor = vbYellow
End With

With Me.TIME_LIMIT.FormatConditions(2)
.BackColor = vbRed
End With

End Sub

RE: VBA conditional formatting

Hi David

Thank you for your question.

This question is rather hard to answer without seeing the database in question.
One possibility would be to run your code from the form's load method rather than the open method.
If this doesn't work please let me know, and I will explore other possibilities.

Regards

Stephen

RE: VBA conditional formatting

Hi David

Thank you for your question.

This question is rather hard to answer without seeing the database in question.
One possibility would be to run your code from the form's load method rather than the open method.
If this doesn't work please let me know, and I will explore other possibilities.

Regards

Stephen

RE: VBA conditional formatting

Hi Stephen

Thanks for looking into this for me. I've used both on open and on load method but both produce the same results.

In terms of the db; its actually just a test db to test the conditional formatting before I apply it to the main db. The table the form is looking at is 2 columns by 7 rows. Column 1 is the ID (1-7) and column 2 is a date field containing 7 dates that are around DATE, even to the extreme of one at 1974 and one at 2020.

As stated, the ID formatting works on the threshold of 4 in the code but the date is purely red.

Thanks

David

RE: VBA conditional formatting

Hi Stephen

I've had time to play around with this now, it looks like the conditional formatting is based on numbers only so I've done a CLng(DATE) to get the 40240 for today and the date field is now formatting red, yellow and green.

Thanks for your time on this one.

Regards

David

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Access tip:

Display current date & time in column of any width

The worksheet function =NOW() returns the current date & time. When entered into a column which is not wide enough to display the value NOW returns, the cell displays ###

View all Access hints and tips


Server loaded in 0.07 secs.