469,330 Members | 1,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

change background color if record number is odd?

AccessIdiot
493 256MB
I know this one is kind of odd. But I was wondering if it were possible to apply some kind of conditional formatting where the background color of the form changes from one record to the next. Something like odd numbers could be the standard grey and even numbers could be pink or something like that.

Is this even possible?

thanks!
Apr 9 '07 #1
14 17861
MMcCarthy
14,534 Expert Mod 8TB
I know this one is kind of odd. But I was wondering if it were possible to apply some kind of conditional formatting where the background color of the form changes from one record to the next. Something like odd numbers could be the standard grey and even numbers could be pink or something like that.

Is this even possible?

thanks!
conditional formatting will only work on active controls that contain values like textboxes. If the form is not continuous you could code something into the on current even of the form but in a continuous form it would change all the background colours each time.

Mary
Apr 10 '07 #2
pks00
280 Expert 100+
As Mary says, use the form_current

Form doesnt have a background colour though, one I think u cant set anyway, though I could be wrong :)

U could set the backcolor for Detail
eg

Expand|Select|Wrap|Line Numbers
  1. private sub Form_Current()
  2.     if me.somefield and 1 = 1 then
  3.        detail.backcolor = vbred      'odd
  4.     else
  5.        detail.backcolor = 16777215  'even
  6.     end if
  7. end sub
  8.  

But the colour u pick, u need to ensure form is still readable
u may find labels have different backgrounds as well, so it could be a case of going thru all controls that need colouring
Apr 10 '07 #3
Rabbit
12,516 Expert Mod 8TB
If you're using 2003 then:
Shading alternate rows in reports
Apr 10 '07 #4
AccessIdiot
493 256MB
As Mary says, use the form_current

Form doesnt have a background colour though, one I think u cant set anyway, though I could be wrong :)

U could set the backcolor for Detail
eg

Expand|Select|Wrap|Line Numbers
  1. private sub Form_Current()
  2.     if me.somefield and 1 = 1 then
  3.        detail.backcolor = vbred      'odd
  4.     else
  5.        detail.backcolor = 16777215  'even
  6.     end if
  7. end sub
  8.  

But the colour u pick, u need to ensure form is still readable
u may find labels have different backgrounds as well, so it could be a case of going thru all controls that need colouring
Hey could you explain that code for me a bit? I'm not sure what is meant by me.somefield - is this any textfield control on the form? And what is 1=1? What does that mean to Access?

thanks!
Apr 10 '07 #5
pks00
280 Expert 100+
ok, code was an example, based on your example of odd numbers

this is an example based on a value in your form

me.somefield refers to a control on your form
detail is the part of the form where all your controls exist

anding a numerical value with 1 is a way to check if its odd number or not


Expand|Select|Wrap|Line Numbers
  1. private sub Form_Current()
  2.  
  3.     'check if field on form called somefield is odd
  4.     if me.somefield and 1 = 1 then
  5.        detail.backcolor = vbred      'odd
  6.     else
  7.        detail.backcolor = 16777215  'even
  8.     end if
  9. end sub
  10.  
Apr 10 '07 #6
AccessIdiot
493 256MB
thanks, that's what I figured but I wanted to check if the 1=1 part referred to the value of somefield or if it was totally unrelated.
Apr 10 '07 #7
Rabbit
12,516 Expert Mod 8TB
If you're using 2003 then:
Shading alternate rows in reports
Just thought I'd point out that this link outlines the steps to do what you want to do.
Apr 10 '07 #8
AccessIdiot
493 256MB
Sorry! I meant to get back to you when I had a chance to check it out. Does it matter that I want to do this in a form and not in a report?

I'll look into it now.
Apr 10 '07 #9
Rabbit
12,516 Expert Mod 8TB
Sorry! I meant to get back to you when I had a chance to check it out. Does it matter that I want to do this in a form and not in a report?

I'll look into it now.
Oh, a form? Totally missed that.
Hmm.. maybe, you might have to use a different event though.
Apr 10 '07 #10
AccessIdiot
493 256MB
Yes I tried adapting it but I can't figure out how to make it work. :)

I tried just copying and pasting the code in, then calling it from the form_current event, but it wants parameters and I have no idea what parameters to give it!

Expand|Select|Wrap|Line Numbers
  1. Private shadeNextRow As Boolean
  2. Const shadedColor = 12632256
  3. ' Const shadedColor = 15726583 ' alternative shade colors
  4. ' Const shadedColor = 14078404
  5. ' Const shadedColor = 13356495
  6. ' Const shadedColor = 14281974
  7. Const normalColor = 16777215
  8.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
  2.  
  3.   On Error GoTo Detail_Format_Error
  4.  
  5. ' Choose a color based on the shadeNextRow value
  6.   If shadeNextRow = True Then
  7.     Me.Section(acDetail).BackColor = shadedColor
  8.   Else
  9.     Me.Section(acDetail).BackColor = normalColor
  10.   End If
  11.  
  12. ' Switch the color for the next row
  13.   shadeNextRow = Not shadeNextRow
  14.  
  15. Detail_Format_Exit:
  16.   Exit Sub
  17.  
  18. Detail_Format_Error:
  19.   MsgBox "Error " & Err.Number & ": " & Err.Description
  20.   Resume Detail_Format_Exit
  21. End Sub
  22.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. Call Detail_Format
  3. End Sub
Apr 10 '07 #11
ADezii
8,800 Expert 8TB
I know this one is kind of odd. But I was wondering if it were possible to apply some kind of conditional formatting where the background color of the form changes from one record to the next. Something like odd numbers could be the standard grey and even numbers could be pink or something like that.

Is this even possible?

thanks!
You could use the 'Read Only' property of a Form in its Current() Event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. If Me.CurrentRecord Mod 2 = 0 Then           'Even Record
  3.   Me.Section(acDetail).BackColor = QBColor(13)
  4. Else
  5.   Me.Section(acDetail).BackColor = QBColor(12)
  6. End If
  7. End Sub
Apr 10 '07 #12
AccessIdiot
493 256MB
Brilliant! That works great! Except the colors are a bit, um, extreme. I know custom colors are not easy in Access but is there a way to get away from the standard color choices?

And could you explain how the "read only" part comes in? I understand the code but am not sure what you meant by "read only".

Thanks so much! :)
melissa
Apr 11 '07 #13
ADezii
8,800 Expert 8TB
Brilliant! That works great! Except the colors are a bit, um, extreme. I know custom colors are not easy in Access but is there a way to get away from the standard color choices?

And could you explain how the "read only" part comes in? I understand the code but am not sure what you meant by "read only".

Thanks so much! :)
melissa
I apologize for the confusing information:
  1. CurrentRecord is a 'Read Only' property of a Form, meaning that it can only be read and not written to.
  2. The Color choices were of course ugly, but they were for demonstration purposes only.
  3. You can have your choice of literally 16,000,00 different colors via the RGB(red, green, blue) Function as indicated below.
Expand|Select|Wrap|Line Numbers
  1. Me.Section(acDetail).BackColor = RGB(100, 213, 4)
  2. Me.Section(acDetail).BackColor = RGB(1 - 255, 1 - 255, 1 - 255)
Place this code in the DblClick() Event of a Form's Detail Section for a simple demo:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_DblClick(Cancel As Integer)
  2.   Randomize
  3.   Me.Section(acDetail).BackColor = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
  4. End Sub
Apr 11 '07 #14
AccessIdiot
493 256MB
Very cool. I knew there must be a way to specify RGB rather than the number off the color palette. Thank you so much!!

:)
Apr 11 '07 #15

Post your reply

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

Similar topics

4 posts views Thread by Georges Heinesch | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.