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

Changing the Color of the Rows of a Form Based on Values Stored in a Table (Access VB

14
Hello,
I am currently working on an access based application where I need to change the color of some specific rows of a form (the color of "one" field per row needs to be changed) based on values stored in these fields. For instance, when we open the form, we need to see color green in the field of a certain row where we have the value 1. If the value of this field is 2, we need to see color orange and if it is 3 it is red.

Example of the output of the form
------------------------------------

ID | Criteria
201 | 1 --> the cell containing 1 should be colored in green
203 | 3 --> the cell containing 3 should be colored in red
204 | 3
205 | --> the cell that contains nothing should be kept uncolored
206 | 1
207 | 2


Note: the values (1, 2 and 3) are already available when the form opens and they are also stored in a table.


Thank you for your help!
Jul 2 '15 #1
4 1511
jforbes
1,107 Expert 1GB
JM11,
Conditional Formatting should do the trick: https://support.office.com/en-sg/art...5-44e43b05e22f

Conditional Formatting can be a little quirky, but it can accomplish color coding that can't be accomplished in any other way natively in Access.
Jul 2 '15 #2
zmbd
5,501 Expert Mod 4TB
Microsoft Tech Support: Change the appearance of a control by using conditional formatting

If that doesn't work then we'll need some VBA or Macro to do the magic.
Jul 2 '15 #3
JM11
14
Thank you so much for your replies,
I actually did conditional formatting and the issue was solved!
Attached is the code that might be helpful if anyone has the same question.

Regards,

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Const GreenG As Byte = 1
  5.  
  6. Const OrangeO As Byte = 2
  7.  
  8. Const RedR As Byte = 3
  9.  
  10.  
  11. Private Sub StartCondFormatting()
  12.  
  13. Dim objFrc As FormatCondition
  14.  
  15.     Const orange As Long = 42495
  16.  
  17.     Const green As Long = 25600
  18.  
  19.     Const red As Long = 255
  20.  
  21.     Dim ctl As Control
  22.  
  23.  
  24.  
  25.     For Each ctl In Me.Controls
  26.  
  27.         If ctl.Tag = "Conditional" Then
  28.  
  29.             With ctl
  30.  
  31.                 'Remove format conditions
  32.  
  33.                 .FormatConditions.Delete
  34.  
  35.  
  36.  
  37.                 'Create three format objects and add them to the FormatConditions
  38.  
  39.                 Set objFrc = .FormatConditions.Add(acExpression, acEqual, "[fieldName] = " & GreenG)
  40.  
  41.                 Set objFrc = .FormatConditions.Add(acExpression, acEqual, "[fieldName] = " & OrangeO)
  42.  
  43.                 Set objFrc = .FormatConditions.Add(acExpression, acEqual, "[fieldName] = " & RedR)
  44.  
  45.  
  46.  
  47.                 'Specify the formating conditions
  48.  
  49.                 .FormatConditions(0).BackColor = green
  50.  
  51.                 .FormatConditions(0).Enabled = True
  52.  
  53.                 .FormatConditions(1).BackColor = orange
  54.  
  55.                 .FormatConditions(1).Enabled = True
  56.  
  57.                 .FormatConditions(2).BackColor = red
  58.  
  59.                 .FormatConditions(2).Enabled = True
  60.  
  61.             End With
  62.  
  63.         End If
  64.  
  65.     Next ctl
  66.  
  67.     Set objFrc = Nothing
  68.  
  69.  
  70.     End Sub
  71.  
  72.  
  73.  
  74. Private Sub Form_Load()
  75. StartCondFormatting        
  76. End Sub


https://accessexperts.com/blog/2011/...ng-using-code/
Jul 2 '15 #4
zmbd
5,501 Expert Mod 4TB
Glad you found a solution; however, IF you follow the link provided, and you are running one of the newer versions of Access (post v97), you do not need any VBA code for many conditional formatting options. For the average user this is the route I recommend... however, the code approach is so much more flexible :)
-z
Jul 2 '15 #5

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

Similar topics

2
by: PerryC | last post by:
Scenario: FormA: Field1, Field2 (Field1 is a combo box based on a NameTable w/ Name and phone fields) When users choose Field1 with Name1 (in record1 of NameTable), Field2 will...
1
by: Richard Dixson | last post by:
Under ASP.NET (C#) I want to create a page that people can use to submit questions. This page will consist of a table with several rows of information, like: Name: Bill Smith Job title:...
1
by: gblakewood | last post by:
Simple question (I think)... I have a form based on a table and I want it to open with it sorted on Last Name ascending. I set the order by property for the form to: tblPersonnel. ASC but it...
6
by: Markus_989 | last post by:
I have a LOANS table that has a list of loan details for different borrowers. I have a main switchboard with a LOANSELECT combo box (that displays a list of borrower last names and loan numbers)....
0
by: perdijc | last post by:
The wizard to create dataset, automatically creates a method to update table if the dataset is based on table. If i create a dataset based on sotored procedure where this is based on more one...
0
by: Aaron Lawrence | last post by:
The company I work for currently uses a number of excel workbooks for asset tracking, job reports, ect. This system is very clumsy and requires a bunch of redundant data entry. I started a pet...
8
by: Shannon Mathews | last post by:
Ok trying to explain... 1. I have a tblMain that holds the master list of all my checks. 2. I'm currently using a make table query "qryCheckHoldingListCreateTbl" to get a list of all the checks...
3
by: Prerana | last post by:
5 links are there in menu(master page of c#).Now they all links are in white color. if i click on first link it wil change to red and remaining to blue. when i click it wil redirect to one page....
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:
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
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,...
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.