473,398 Members | 2,525 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,398 software developers and data experts.

How does one apply conditional formatting to a textbox in a subreport through code?

Hello!
I am trying to apply conditional formatting to several subreports within a main report (Charlie) with code - because I have four conditions.

When the value of txt_SafetyFundingNumber is
1-5, red fill with white font;
6-9, yellow fill with black font;
10-14 blue fill with white font; and
15-20, green fill with white font.

The following is my attempt to apply a red fill with white font when the value of the textbox is <=5.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
  2.     ' Set color coding of field based on value
  3.  
  4.     Dim intSafetyFundNum As Integer
  5.  
  6.     If Not IsNull(Me.txt_SafetyFundingNumber.Value) Then
  7.         intSafetyFundNum = Me.txt_SafetyFundingNumber.Value
  8.     Else
  9.         Exit Sub
  10.     End If
  11.  
  12.     If intSafetyFundNum <= 5 Then
  13.     Me.txt_SafetyFundingNumber.Report.FillColor = 3
  14.     Me.txt_SafetyFundingNumber.Report.ForeColor = 1
  15.  
  16.     End If
  17.  
  18. End Sub
  19.  
I've attached the zipped db to take a peek. This is the last piece of a very long and painful process for a newbie! Every time I complete one piece, I hit another wall........I guess this is trial-by-fire?
Thanks so much for any advice anyone cares to give!
:)
Attached Files
File Type: zip Site_Safety_App.zip (1.02 MB, 93 views)
May 5 '10 #1
8 2525
This is what I found this morning and then modified for my own purposes, which was so much simpler than I thought! It seems to be working. :)
Expand|Select|Wrap|Line Numbers
  1. If txtSafeFundNum <= 5 Then
  2. txtSafeFundNum.BackColor = vbRed
  3. txtSafeFundNum.ForeColor = vbWhite
  4. ElseIf txtSafeFundNum <= 9 And txtSafeFundNum > 5 Then
  5. txtSafeFundNum.BackColor = vbYellow
  6. txtSafeFundNum.ForeColor = vbBlack
  7. ElseIf txtSafeFundNum <= 14 And txtSafeFundNum > 9 Then
  8. txtSafeFundNum.BackColor = vbBlue
  9. txtSafeFundNum.ForeColor = vbWhite
  10. ElseIf txtSafeFundNum <= 20 And txtSafeFundNum > 14 Then
  11. txtSafeFundNum.BackColor = RGB(51, 102, 0)
  12. txtSafeFundNum.ForeColor = vbWhite
  13. Else
  14. txtSafeFundNum.BackStyle = 1
  15. txtSafeFundNum.ForeColor = vbBlack
  16. End If
May 6 '10 #2
NeoPa
32,556 Expert Mod 16PB
May I ask why the Conditional Formatting is not applied in the design of the reports (The reports that are used as subreports in your main report that is) themselves?
May 6 '10 #3
NeoPa
32,556 Expert Mod 16PB
By the way, the concept you are describing is Conditionally Applied Formatting, as opposed to Conditional Formatting. This will work for some instances, but when the form is in continuous (or Datasheet) view mode, you will find problems (See Why Values in Unbound Form Controls do not Persist).
May 6 '10 #4
@NeoPa
Unless I'm mistaken, which could very well be, the code does reside in the On Format function of the detail in each (sub)report. Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
  3.  
  4. On Error Resume Next
  5.  
  6. txtSafeFundNum.BackStyle = 1
  7.  
  8. If txtSafeFundNum <= 5 Then
  9. txtSafeFundNum.BackColor = vbRed
  10. txtSafeFundNum.ForeColor = vbWhite
  11. ElseIf txtSafeFundNum <= 9 And txtSafeFundNum > 5 Then
  12. txtSafeFundNum.BackColor = vbYellow
  13. txtSafeFundNum.ForeColor = vbBlack
  14. ElseIf txtSafeFundNum <= 14 And txtSafeFundNum > 9 Then
  15. txtSafeFundNum.BackColor = vbBlue
  16. txtSafeFundNum.ForeColor = vbWhite
  17. ElseIf txtSafeFundNum <= 20 And txtSafeFundNum > 14 Then
  18. txtSafeFundNum.BackColor = RGB(51, 102, 0)
  19. txtSafeFundNum.ForeColor = vbWhite
  20. Else
  21. txtSafeFundNum.BackStyle = 0
  22.  
  23. End If
  24.  
  25. End Sub
  26.  
I cannot believe how much I'm learning on-the-fly, thanks to everyone in these forums! It may not be the cleanest or the most efficient, but I have much to learn yet! ;)
May 6 '10 #5
@NeoPa
Thank you for the warning! This drill is to automate the reports to reduce my hands-on time. I haven't created the input forms for the users yet, but that is my next step - so I may run into problems when I do. I'll be sure to refer to the information you've provided.
I've also thought about creating CF pages to allow for user entry and report automation, but that will be another project entirely! :)
May 6 '10 #6
NeoPa
32,556 Expert Mod 16PB
I would certainly recommend you use Conditional Formatting within your report. In design mode select Format (menu) / Conditional Formatting... then configure the control as you require. This will avoid the problems associated with persisting controls and is also easier to do. Avoiding the requirement for code as it does.
May 6 '10 #7
Except that I need 4 conditions, and the built-in Conditional Formatting tool is limited to 3. I wish it had been that easy! :)
May 6 '10 #8
NeoPa
32,556 Expert Mod 16PB
That's a bit of a pain. I suspect you're spot on with that though (except it supports 4 & you have 5 - including the default (or rest) of course. Although you nowhere say how you want >20 handled).

Conditionally Applied Formatting should work for you then, but it means you must apply the formatting at format time for each line. This you are doing, so all should be well.

BTW If >20 is not possible we can use Conditional Formatting with 3 specified conditions and the original format as a catch-all.
May 6 '10 #9

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

Similar topics

4
by: Bradley | last post by:
I have an A2000 database in which I have a continuous form with a tick box. There is also a text box with a conditional format that is based on the expression , if it's true then change the...
2
by: Megan | last post by:
Can you write conditional VBA code that affects only one or two records on a continuous subform? I have a form with a subform on it. The parent/ child field that links the forms is CaseID. The...
8
by: Dimitri Furman | last post by:
Given: Access 2002/2003 A subform in datasheet or continuous view, placed on a tab page (this last may or may not matter) Conditional formatting applied to some controls on the subform - format...
5
by: Andrew Chanter | last post by:
Does anyone know a way you can use conditional formatting to create a banded style view as is commonly seen on the internet. (In othe words the first record appears on a gray background, the 2nd...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works...
0
by: urbancowboy | last post by:
Hi, I'm a complete noob to XSLT. My goal is to apply conditional formatting to the table-cell only so when the date = today, the entire table-cell background will highlight green (#00FF00) and not...
4
by: ApexData | last post by:
In a Continuous form, I want to use Conditional Formatting to change the background color of a date field based on a condition. 1-The color chart in the CF menu option offers limited colors. How...
10
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through...
1
by: Greg (codepug | last post by:
Access 2000 Using a textbox of a single form, I created a calculated field. The following code is in the Control Source for this field: =IIf(=24,+(/),/ ) The numbers that are calculated are...
3
by: MNNovice | last post by:
I looked up the Help key to find out more about conditional formatting in Access. But all I could find is this To apply conditional formatting to a control on a data access page, you need to write 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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.