473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Report Conditional Formating in Access 2007

I'm trying to change the font color of a textbox on a report to red if the date displayed in the textbox is before today. I'm using the following code to accomplish this:

Expand|Select|Wrap|Line Numbers
  1. Dim Ctrl1 as Textbox
  2. Dim CFO As FormatCondition
  3. Dim EXP1str as String
  4.  
  5. EXP1str = "[" & Ctrl1.Name & "]<" & Date
  6.  
  7. Set CFO = Ctrl1.FormatConditions.Add(acExpression, , EXP1str )
  8. CFO.ForeColor = RGB(255, 0, 0)
The problem is the value I'm assigning to "EXP1str" is not being evaluated by Access the way I am expecting. Can anyone tell me how to build the value for EXP1str? Or should the type of condition be set to "acFieldValue" instead of "acExpression"? There's not a lot of guidance in the help files about this.

Thanks in advance for your help!
Jul 15 '08 #1
6 6435
OK, I'm still experimenting trying to figure out this problem. I'm trying to turn the text red in a textbox (on a report) if the date contained in the textbox is before the current date. I've found that if I use the code below, Access evaluates the condition in a way I don't understand. Sometimes the dates before the current date are not turned red and sometimes the dates after the current date are turned red. Ug!

Expand|Select|Wrap|Line Numbers
  1. Dim Ctrl1 as Textbox
  2. Dim CFO As FormatCondition
  3.  
  4. 'The control has the same name as the control source its associated with
  5.  
  6. Set CFO = Ctrl1.FormatConditions.Add(acExpression, , "[" & Ctrl1.Name & "] < Date()")
  7. CFO.ForeColor = RGB(255, 0, 0)
This should be so simple but I'm clearly missing something. Can anyone tell me what I'm doing wrong?
Jul 16 '08 #2
missinglinq
3,532 Expert 2GB
I'll be honest, I haven't got a clue as to what you're trying to do with this code, and I suspect by the lack of responses that no else does either. But this kind thing is easily done using Conditional Formatting, assuming you're running Access 2000 or later.

  1. In Form Design View, click on the textbox to select it
  2. Goto Format - Conditional Formatting and set up the boxes as:
  3. "Field Value Is" "Less Than" Date()
  4. Now, set the font/forecolor to Red
  5. Click "OK"
Linq ;0)>
Jul 16 '08 #3
I'll be honest, I haven't got a clue as to what you're trying to do with this code, and I suspect by the lack of responses that no else does either. But this kind thing is easily done using Conditional Formatting, assuming you're running Access 2000 or later.

  1. In Form Design View, click on the textbox to select it
  2. Goto Format - Conditional Formatting and set up the boxes as:
  3. "Field Value Is" "Less Than" Date()
  4. Now, set the font/forecolor to Red
  5. Click "OK"
Linq ;0)>
Thanks for responding missinglinq. I'm sorry that my post was so confusing. I think, sometimes, longer posts get less attention because folks don't have the time to wade through a ton of irrelavent details. So I try to keep my posts as short as possible so I don't waste people's time. I guess my brevity came at the expense of clarity. My apollogies.

For the problem I'm having, I'm already using the conditional formating. However, instead of using the conditional formatting dialog box by right-clicking on the textbox while the report is in design view, I was setting up the conditional formatting using code in the report's OnOpen event. My report actually has a dozen or more textboxes that I want to set up the exact same conditional formatting. So it seemed to me to be more efficient to use VB code to do it.

In the code I provided earlier, Ctrl1 is the generic name of a textbox on the report. CFO is the name of a FormatCondition object. The following line of code creates a new condition for the textbox:

Expand|Select|Wrap|Line Numbers
  1. Set CFO = Ctrl1.FormatConditions.Add(acExpression, , "[" & Ctrl1.Name & "] < Date()")
I named the textbox (Ctrl1) the same as the control source field (which is a date/time field type). Thus, the third argument of the line of code above should be evaluated as "[Ctrl1] < Date()" where the value of Ctrl1 is some date.

The next line of code sets the text color to red if the condition above is true:
Expand|Select|Wrap|Line Numbers
  1. CFO.ForeColor = RGB(255, 0, 0)
To me, this fairly simple and straight forward. However, like I said earlier, Access is evaluating the condition in some unknow manner. I say this because when my report runs the text for some records where the value of Ctrl1 is before today is not turned red (even though it meets the condition Ctrl1 < Date() ) and the text for some records where the value of Ctrl1 is later than today is turned red.

I wanted some help to figure out how Access is evaluating the condition "Ctrl1 < Date()" so I can adjust the syntaxt to get the result I desire (any date before today is shown in red text).

I'm sorry if I've been too long winded. I'm just trying to avoid confusing folks, again.

Thanks again for your help.
Jul 16 '08 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi. Date comparisons in code can cause difficulties, because unless a control is bound to a date-time field the values need to be coerced to a date type for such comparisons to work consistently.

I tested this by adding a control to a form and performing a comparison of that unbound control to the current date. I used the following debug code in the After Update event of the text control to show the values :

Expand|Select|Wrap|Line Numbers
  1. MsgBox ("Direct test: " & (Me.Text5 < Date) & " CDate test: " & (CDate(Me.Text5) < Date))
The second, type-converted, test consistently gave the correct result, the direct test an incorrect result.

Date/time values are stored internally as numeric values with the integer part the date and the decimal part the time. The date part counts the number of days from a particular reference date (1 Jan 1900 for dates in Microsoft products).

When a date is typed into a control what is displayed by the chosen date format as a human-readable date is quite different to what is stored internally in a date/time field. Although Access does its own type conversions when it can, it can sometimes cause further confusion as Access will flip day and month values to see if it can get a valid date from what has been typed (recognising that the ANSI SQL standard is for dates to be in American m/d/y format which may not be the same as the regional settings for the PC).

This is a long explanation for what may be causing you difficulties. Try a CDate type conversion and see how you get on.

-Stewart
Jul 17 '08 #5
Hi
I would forget conditional formatting in reports when things are complicated

Below lines of code used in the OnPrint event of Detail1

If Me!machStatus = "On Loan" And Me!Days >= 120 And Me!Days < 180 Then
Me!Days.ForeColor = 128
ElseIf Me!machStatus = "On Loan" And Me!Days >= 180 Then
Me!Days.ForeColor = 255
Me!Plus180.ForeColor = 255
Else
Me!Days.ForeColor = 0
Me!Plus180.ForeColor = 0
End If

If Me!RVDate < Date Then
Me!RVDate.ForeColor = 255
Else
Me!RVDate.ForeColor = 0
End If

If Me!Plus180 < Date Then
Me!Plus180.ForeColor = 255
Else
Me!Plus180.ForeColor = 0
End If

If Me!machStatus = "Off Lease" Then Me!RVDate.ForeColor = 255

If Me!machStatus = "On Lease" And Me!Year = DatePart("yyyy", Date) Then
Me.Detail1.BackColor = 10079487
Else
Me.Detail1.BackColor = 16777215
End If

Works a treat

Have fun

Avellano
Jul 17 '08 #6
Hi. Date comparisons in code can cause difficulties, because unless a control is bound to a date-time field the values need to be coerced to a date type for such comparisons to work consistently.

I tested this by adding a control to a form and performing a comparison of that unbound control to the current date. I used the following debug code in the After Update event of the text control to show the values :

Expand|Select|Wrap|Line Numbers
  1. MsgBox ("Direct test: " & (Me.Text5 < Date) & " CDate test: " & (CDate(Me.Text5) < Date))
The second, type-converted, test consistently gave the correct result, the direct test an incorrect result.

Date/time values are stored internally as numeric values with the integer part the date and the decimal part the time. The date part counts the number of days from a particular reference date (1 Jan 1900 for dates in Microsoft products).

When a date is typed into a control what is displayed by the chosen date format as a human-readable date is quite different to what is stored internally in a date/time field. Although Access does its own type conversions when it can, it can sometimes cause further confusion as Access will flip day and month values to see if it can get a valid date from what has been typed (recognising that the ANSI SQL standard is for dates to be in American m/d/y format which may not be the same as the regional settings for the PC).

This is a long explanation for what may be causing you difficulties. Try a CDate type conversion and see how you get on.

-Stewart
BINGO! The CDate function did it. I modified my code to the following and it works perfectly:
Expand|Select|Wrap|Line Numbers
  1. Set CFO = Ctrl1.FormatConditions.Add(acExpression, , "CDate([" & Ctrl1.Name & "].Value) < Date()")
  2. CFO.ForeColor = RGB(255, 0, 0)
Thanks Stewart! And thanks to Missinglinq and Avellano for your help, too.

Scott
Jul 17 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Todd D. Levy | last post by:
I have a query based report where a number of the fields in each record are Yes/No data fields. I would like this report to behave as follows: If ALL of the Yes/No data fields in a given...
2
by: rudy | last post by:
I am trying to highlight the output of a field to display something similar to conditional formating. Would prefer to display the result with a code instead of a colored font or colored background...
3
by: Tom | last post by:
Is there any way to use conditional formating to change a control for a field in a continuous form to a textbox, option group or combobox depending on the value of another field on the form? On a...
3
by: bisogni | last post by:
My dilemma is this: I have to run an access report that makes certain customer records easily viewable in a daily report that is to be run. Is it possible to conditionally shade just the...
1
by: dddsssdddsss | last post by:
A comment and a question To anyone who is using conditional formatting, beware that in Access 2007 the color pallette is not the same as the color pallette in Access 2003. So if you have a...
4
by: slinky | last post by:
Thanks in advance... I have a continuous style form with a field for each record called "STATUS". I simply want to have the form load and if the value of the textbox is "Inactive" I want the...
2
by: FNA access | last post by:
I have a request to highlight any entries in a report that have the same date. The report is sorted by date so the records would be one after the other. I cannot find any events for the report that...
4
by: riaane | last post by:
Please help: I have Conditional Formatting on a Report Field that fills the background Red if the criteria is met. This displays correctly in Report View, however, when I "OutputTo" this report to...
0
by: greylensman | last post by:
I know you can export an Access 2007 report to a Word .rft tab delimited file but that's not what I need. I'd like to get/put together the VBA (if possible) to export the Access Report directly to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.