PC Review


Reply
Thread Tools Rate Thread

Conditional Formatting via Code

 
 
ant1983
Guest
Posts: n/a
 
      18th Jan 2010
Hi,

posted this but couldnt quite figure out the code i was given.

Please can someone help: i have a report and want a certain textbox
numPercentageBooked shaded one of 4 colours depending on the value. Values
below:

0% to 65% = Red
66% to 99% = Yellow
100% = Green
More that 100% = Blue

Was thinking of doing it on the the form load event but have no idea what to
write being a novice

Ta
 
Reply With Quote
 
 
 
 
Dirk Goldgar
Guest
Posts: n/a
 
      18th Jan 2010
"ant1983" <(E-Mail Removed)> wrote in message
news:A4BB51F3-8D9D-4044-967F-(E-Mail Removed)...
> Hi,
>
> posted this but couldnt quite figure out the code i was given.
>
> Please can someone help: i have a report and want a certain textbox
> numPercentageBooked shaded one of 4 colours depending on the value.
> Values
> below:
>
> 0% to 65% = Red
> 66% to 99% = Yellow
> 100% = Green
> More that 100% = Blue
>
> Was thinking of doing it on the the form load event but have no idea what
> to
> write being a novice



Why do this by code? Why not use the built-in conditional formatting? I
know you have four conditions, not three, but one of them can be your
default state. If you had five conditions, *then* you couldn't use
conditional formatting.

Is the field, numPercentageBooked, a floating-point field (single or
double), so it actually has value ranges like these:

0 - .65 --> Red
.65000001 - .99999999 --> Yellow
1.0 - 1.0 --> Green
> 1.0 --> Blue


?

One problem you may run into is that floating-point numbers are inherently
imprecise, especially if they are the result of a calculation. So you need
to decide how close the number must be to one of your boundary values to be
considered equal to it. For example, you might choose to express your
conditions like this:

Null or <=.650001
> .650001 And <= .999999
> .999999 And <= 1.000001
> 1.000001


I picked those values pretty arbitrarily, but you get the idea.

If you want to do this in code, not via conditional formatting, then you
should use the form's Current event to apply the formatting. However, that
will really only work the way you want when the form is in single-form view,
so the user only sees the record that is current. On continuous forms,
where the user sees many records that are not current, conditional
formatting is the only simple solution.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      18th Jan 2010
"Dirk Goldgar" <(E-Mail Removed)> wrote in message
news:B0A113F7-BB11-48F1-8165-(E-Mail Removed)...
>
> If you want to do this in code, not via conditional formatting, then you
> should use the form's Current event to apply the formatting. However,
> that will really only work the way you want when the form is in
> single-form view, so the user only sees the record that is current. On
> continuous forms, where the user sees many records that are not current,
> conditional formatting is the only simple solution.


CORRECTION
I just realized you said "report", not "form". Therefore the above
paragraph is totally wrong. For a report, to do it in code use the
Detail_Format event, and don't worry about what I said regrding the
continuous forms and current records. For example,

'------ start of example code ------
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

With Me.numPercentageBooked
Select Case .Value
Case 0 To 0.650001: .BackColor = vbRed
Case Is <= 0.999999: .BackColor = vbYellow
Case Is <= 1.000001: .BackColor = vbGreen
Case Is > 1.000001: .BackColor = vbBlue
Case Else: .BackColor = vbRed
End Select
End With

End Sub
'------ end of example code ------

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
ant1983
Guest
Posts: n/a
 
      19th Jan 2010
Er,..... Wheres the detail format event?

"Dirk Goldgar" wrote:

> "Dirk Goldgar" <(E-Mail Removed)> wrote in message
> news:B0A113F7-BB11-48F1-8165-(E-Mail Removed)...
> >
> > If you want to do this in code, not via conditional formatting, then you
> > should use the form's Current event to apply the formatting. However,
> > that will really only work the way you want when the form is in
> > single-form view, so the user only sees the record that is current. On
> > continuous forms, where the user sees many records that are not current,
> > conditional formatting is the only simple solution.

>
> CORRECTION
> I just realized you said "report", not "form". Therefore the above
> paragraph is totally wrong. For a report, to do it in code use the
> Detail_Format event, and don't worry about what I said regrding the
> continuous forms and current records. For example,
>
> '------ start of example code ------
> Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
>
> With Me.numPercentageBooked
> Select Case .Value
> Case 0 To 0.650001: .BackColor = vbRed
> Case Is <= 0.999999: .BackColor = vbYellow
> Case Is <= 1.000001: .BackColor = vbGreen
> Case Is > 1.000001: .BackColor = vbBlue
> Case Else: .BackColor = vbRed
> End Select
> End With
>
> End Sub
> '------ end of example code ------
>
> --
> Dirk Goldgar, MS Access MVP
> Access tips: www.datagnostics.com/tips.html
>
> (please reply to the newsgroup)
>

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      19th Jan 2010
"ant1983" <(E-Mail Removed)> wrote in message
news:440DAE10-357A-43AD-A959-(E-Mail Removed)...
> Er,..... Wheres the detail format event?



It's an event for the report's Detail section. In design view, if you
select the Detail section and open its property sheet, on the Event tab of
the property sheet you'll find the "On Format" property where you can set
behavior for the Detail event.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      19th Jan 2010
"Dirk Goldgar" <(E-Mail Removed)> wrote in message
news:9E06AE89-A1EF-48B3-8467-(E-Mail Removed)...
> "ant1983" <(E-Mail Removed)> wrote in message
> news:440DAE10-357A-43AD-A959-(E-Mail Removed)...
>
> I[...] on the Event tab of the property sheet you'll find the "On Format"
> property where you can set behavior for the Detail event.



Sorry, that should have been "... for the Format event."

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
ant1983
Guest
Posts: n/a
 
      19th Jan 2010
LOL Thanks...

"Dirk Goldgar" wrote:

> "ant1983" <(E-Mail Removed)> wrote in message
> news:440DAE10-357A-43AD-A959-(E-Mail Removed)...
> > Er,..... Wheres the detail format event?

>
>
> It's an event for the report's Detail section. In design view, if you
> select the Detail section and open its property sheet, on the Event tab of
> the property sheet you'll find the "On Format" property where you can set
> behavior for the Detail event.
>
> --
> Dirk Goldgar, MS Access MVP
> Access tips: www.datagnostics.com/tips.html
>
> (please reply to the newsgroup)
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Formatting -- Need VBA Code Kim M. Microsoft Access VBA Modules 1 15th Mar 2010 08:53 PM
Code for conditional formatting =?Utf-8?B?Q2FsZWRvbmlh?= Microsoft Access Form Coding 13 1st Mar 2007 12:03 AM
Code for conditional formatting =?Utf-8?B?cm9uIGI=?= Microsoft Access 5 20th Jan 2007 09:16 AM
Code to do conditional formatting sc888ter Microsoft Access Form Coding 1 26th Mar 2005 05:21 AM
Conditional Formatting Code ? Gulf Coast Electric Microsoft Access Forms 3 9th Apr 2004 07:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:55 PM.