Assisgn color to -ve number

S

SF

Hi,

In my report, I have several negative number. I want to highlight the
negative number to Red by using the following code.

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.txtNoRecord < 0 Then
Me.txtNoRecord.BackColor = 255
End If

I did not get result as expected.

SF
 
J

John Spencer (MVP)

You need to set the color to white if the number is greater than or equal to zero.

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.txtNoRecord < 0 Then
Me.txtNoRecord.BackColor = vbRed
else
Me.txtNoRecord.Backcolor = vbWhite
End If

An alternative is to use Conditional Formatting (if it is available in your
version of Access).
-- Click on the control
-- Select Format: Conditional formatting ... from the menu
-- Set Condition 1 to
Field Value is
Less Than
0
-- Click on the paint bucket drop down and select a color for the background
when the condition is met

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
M

Marshall Barton

SF said:
In my report, I have several negative number. I want to highlight the
negative number to Red by using the following code.

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.txtNoRecord < 0 Then
Me.txtNoRecord.BackColor = 255
End If


There are several ways to do that. Your way, using code,
requires that you set the BackColor for both positive
numbers too:

If Me.txtNoRecord < 0 Then
Me.txtNoRecord.BackColor = 255
Else
Me.txtNoRecord.BackColor = vbWhite
End If

But, there are two other ways that do not require any code.
One, only good for the basic colors, is to set the text
box's Format property to something like:
0;[Red]-0;0
or whatever style yu need for you type of numbers.

Another is to use Conditional Formatting (Format menu) with
the condition:
<0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top