Text field on report

  • Thread starter Thread starter Jim madock
  • Start date Start date
J

Jim madock

I would like to have a text field (numeric data) change to
red if the value is a negitive number
 
Click on the Section (not on a control) that has the text box in Design view
of the report.Click the Properties button on the toolbar.
Go to Events, Click next to OnFormat and choose Event Procedure. Click just
right of that to open a code page.

Type between the lines of code already there so that you get something like

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Nz(DCount("[Num]", "TblTestOnly")) <> 0 Then
'check there are records in the report
'or you will get an error
If Me.Num < 0 Then
Me.Num.BackColor = 255
Else
Me.Num.BackColor = 16777215
End If
End If
End Sub

Where I've written Num and TblTestOnly, replace those with the names of a
field and Table that has the records.

In the Properties Format tab for the control itself make sure that BackStyle
is set to Normal, not Transparant.

If you wanted the Font, not the back colour to change then change BackColor
to ForeColor in the code.

One thing, I notice that you say you have a 'text field with numeric data'.
If you really mean that and it wasn't a slip of the pen then create a field
in the Report's query and in it type

NumVal: Val([YourTextField])
Use this and not the text field to display your data and look for negative
values and put its name in the code.

Evi
 
Jim said:
I would like to have a text field (numeric data) change to
red if the value is a negitive number
Jim,
In all access versions, if the field is a Text datatype field you can
use code, in the Report Section's Format event:

If [[YourField] < 0 Then
[YourField].ForeColor = vbRed
Else
[YourField].ForeColor = vbBlack
End If

If you have Access 2000 or later, you can also use the control's
Conditional Formatting property
Format + Conditional Formatting

Set the 'Field Value Is' drop-down expression to Less Than
Set the Value as 0
Set the controls Color's as desired.

If you are using a field that is a Number datatype, then all you would
need do is write (in all versions) is:
#[Black];-#[Red];0
in the control's Format property

Hope this has helped.
 
Back
Top