473,395 Members | 2,079 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.

Loop over rows of a form (Access VBA) Conditional Formatting

14
Hello,
I would like to loop over the rows of a form and check in each row the value of 1 column; depending on this value, the cell (for the corresponding row and column) should change color. This procedure should be done when the form is opened so that we can see the colored cells as soon as we open the form.
Thank you for your help,
Regards
Jul 2 '15 #1

✓ answered by MikeTheBike

Hi

Have you considered conditional formating?
This would seem to be the application for which it is designed!

If you need any help with that then let us know.

MTB

4 2366
MikeTheBike
639 Expert 512MB
Hi

Have you considered conditional formating?
This would seem to be the application for which it is designed!

If you need any help with that then let us know.

MTB
Jul 2 '15 #2
JM11
14
Thank you so much for your reply,
I did not know about conditional formatting (I am new to access VBA); I did a search on it and now it is working perfectly!
Attached is the code; it could be helpful for people who might have 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 #3
MikeTheBike
639 Expert 512MB
I am glad that helped, but I must confess I have never seen or considered doing it in code. That in itself is very interesting.
I have always set conditional formatting in design view.

As they say, you learn something new every day (well almost every day!).

MTB
Jul 3 '15 #4
NeoPa
32,556 Expert Mod 16PB
Up until 2003 there were only ever three settings for Conditional Formatting (2007 increased that to a more useful number). In that situation it made sense for various experts to develop and share code-based techniques that could do a similar job.

Since 2007 they're now mostly redundent.
Jul 3 '15 #5

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

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
7
by: Shaldaman | last post by:
I have an Access form called "Login". "Command10" is a button on the form and "Text5" is a text field on it. When a user enters a value in the Text5 text field and clicks the button Command10, I...
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...
7
by: wongray | last post by:
Hi, I have an access question. it is possible to set a formula in access to check the current date - the date field.? Kindly advise. Regards Raymond
1
by: DAObermeyer | last post by:
Hey all, first time here. I have a database that is designed to be opened and viewed at other locations on the network. I use Form (A) to modify Data in Table (A), and Form (B) is used scrictly...
3
by: Rhys Gottwald | last post by:
Hi All, I have a report that is going to have 30 odd fields each containing a number, the number is the wind speed. I want to format the background of the field based on the value of the...
4
by: JM11 | last post by:
Hello, I would like to display the current date (in a field called "Latest date" in a form) when a button is pressed in that form. The field in the form has a source in a table; thus, I am trying to...
3
by: JM11 | last post by:
Hello, I would like to replace the character 'Â' with an empty space for all the values that belong to the column entitled 'Montant' of a form. Below is the code that I wrote but is not working. Any...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.