473,469 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

14 New Member
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 2369
MikeTheBike
639 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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 Recognized Expert Moderator MVP
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
1
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...
0
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...

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.