473,769 Members | 4,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Report Conditional Formating in Access 2007

69 New Member
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 "acFieldVal ue" instead of "acExpressi on"? There's not a lot of guidance in the help files about this.

Thanks in advance for your help!
Jul 15 '08 #1
6 6471
sphinney
69 New Member
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 Recognized Expert Specialist
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
sphinney
69 New Member
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 Recognized Expert Moderator Specialist
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
Avellano
7 New Member
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.ForeCol or = 128
ElseIf Me!machStatus = "On Loan" And Me!Days >= 180 Then
Me!Days.ForeCol or = 255
Me!Plus180.Fore Color = 255
Else
Me!Days.ForeCol or = 0
Me!Plus180.Fore Color = 0
End If

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

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

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

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

Works a treat

Have fun

Avellano
Jul 17 '08 #6
sphinney
69 New Member
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
1496
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 record have a value of YES, I would like that particular record to be suppressed (I.e. NOT appear in the report). and
2
1645
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 for the sake of using a black and white printer. i.e. if >= 20 then H for highly skilled or >= 15 then S for Skilled Even though the colors work there is only three options to select from under Conditional Formatiting (unless there's a way...
3
1792
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 continuous form, I need some records to have a textbox, some to have an option group and some to have a combobox in the same field. Thanks, Tom
3
1706
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 background for certain individual customers (lets say possibly a dozen different customers which may vary daily), leaving the remaining report unshaded? The only shading info I can find is the "every other line" shading which won't help me. I was...
1
4232
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 particular color you were using as a standard throughout your application, the only way to continue using that color is to code the color. Anyone else notice how forms in Access 2007 display the conditional formatting much slower than in Access...
4
8978
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 textbox background to display yellow, else I want it to be just white. Here's my code... but it is not working Private Sub Form_Load() If Me!STATUS = "Inactive" Then Me!STATUS.BackColor = RGB(255, 255, 0) Else
2
1554
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 I would be able to add any code to. Any input or advice would be appreciated I would like to figure the code myself as I find I learn better that way, I just can't find an event to put the code into. My report is based on a saved query,...
4
5651
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 "Rich Text Format (*.rtf)", the colour is lost on the resultant RTF document when I open it up in Word 2007. I would obviously like to retain the colours dictated by my Conditional Formatting criteria results when I output this report to RTF format....
0
2081
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 Word.docx table. Looked around but all the references just mention the .rft or a .doc with some retention of formating but not a table.
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10038
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.