Full Version: Changing fonts after comparing the records
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
Tamrak
Is there anyway we can change the fonts on the report when we see any different after comparing the previous record versus the current record?

I am uncertain whether the conditional format will be working.

Example, on my tblProject.

I would like the report to look like the following:

Project,ActivityDate,Score1,Score2,Score3

1234, 12/05/2006, 1.5, 2.5, 3.5
1234, 12/08/2006, 1.5, 2.7 , 3.5
1234, 12/10/2006, 1.7 , 2.7, 3.5
1800, 12/1/2006, 2.5, 3.5, 3.8
1800, 12/3/2006, 2.5, 3.7 , 3.4

1. After comparing the previous record, under the same project number , if there are any changes, the font will be changed to red.
2. If the project number changes, for example, from 1234 to 1800 (line # 3 and # 4), there will be no comparison.

I do appreciate all suggestions that you might have. Thanks.
BrianS
There is no "pretty" way to do this. A very ugly way would be this would be something like this

*untested*

CODE
Public Sub Detail_Print()

[color="green"]'Static variables hold their Values after Sub completes.
'Variants are used so they can hold either numeric, text, or nulls  [/color]
Static i as Long
Static ProjectNum as Variant
Static Score1 as Variant
Static Score2 as Variant
Static Score3 as Variant

i = i + 1

[color="green"]'Don't want first record to be red  [/color]
If i = 1Then
   ProjectNum = Me.txtProjectNum
   Score1 = Me.txtScore1
   Score2 = Me.txtScore2
   Score3 = Me.txtScore3
End If

If ProjectNum <> Me.txtProjectNum Then
   If Score1 <> Me.txtScore1 Then
      Me.txtScore1.ForeColor = vbRed
   Else
      Me.txtScore1.ForeColor = vbBlack
   End If
   If Score2 <> Me.txtScore2 Then
      Me.txtScore2.ForeColor = vbRed
   Else
      Me.txtScore2.ForeColor = vbBlack
   End If
   If Score3 <> Me.txtScore3 Then
      Me.txtScore3.ForeColor = vbRed
   Else
      Me.txtScore3.ForeColor = vbBlack
   End If
End If

[color="green"]'convert because you can't compare nulls
'(null = null) <> True
'(null = null) = null  [/color]
ProjectNum = Nz(Me.txtProjectNum,"")
Score1 = Nz(Me.txtScore1,"")
Score2 = Nz(Me.txtScore2,"")
Score3 = Nz(Me.txtScore3,"")

End Sub
BrianS
I caught a couple logic errors, try this code instead

CODE
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)


[color="green"]'Static variables hold their Values after Sub completes.
'Variants are used so they can hold either numeric, text, or nulls[/color]
Static i As Long
Static ProjectNum As Variant
Static Score1 As Variant
Static Score2 As Variant
Static Score3 As Variant

i = i + 1

[color="green"]'Don't want first record to be red[/color]
If i = 1 Then
   ProjectNum = Me.txtProjectNum
   Score1 = Me.txtScore1
   Score2 = Me.txtScore2
   Score3 = Me.txtScore3
End If

If ProjectNum = Me.txtProjectNum Then
   If Score1 <> Me.txtScore1 Then
      Me.txtScore1.ForeColor = vbRed
   Else
      Me.txtScore1.ForeColor = vbBlack
   End If
   If Score2 <> Me.txtScore2 Then
      Me.txtScore2.ForeColor = vbRed
   Else
      Me.txtScore2.ForeColor = vbBlack
   End If
   If Score3 <> Me.txtScore3 Then
      Me.txtScore3.ForeColor = vbRed
   Else
      Me.txtScore3.ForeColor = vbBlack
   End If
Else
    Me.txtScore1.ForeColor = vbBlack
    Me.txtScore2.ForeColor = vbBlack
    Me.txtScore3.ForeColor = vbBlack
End If

[color="green"]'convert because you can't compare nulls
'(null = null) <> True
'(null = null) = null[/color]
ProjectNum = Nz(Me.txtProjectNum, "")
Score1 = Nz(Me.txtScore1, "")
Score2 = Nz(Me.txtScore2, "")
Score3 = Nz(Me.txtScore3, "")

End Sub
Tamrak
I tried this code and it was working 90%

Option Compare Database
Dim lngProject As Long
Dim dblScore1 As Double
Dim dblScore2 As Double
Dim dblScore3 As Double

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If IsEmpty(lngProject) Or lngProject <> [Project] Then
Me.Score1.FontItalic = False
Me.Score1.ForeColor = vbBlack
Me.Score2.FontItalic = False
Me.Score2.ForeColor = vbBlack
Me.Score3.FontItalic = False
Me.Score3.ForeColor = vbBlack
Else
If dblScore1 <> [Score1] Then
Me.Score1.FontItalic = True
Me.Score1.ForeColor = vbRed
Else
Me.Score1.FontItalic = False
Me.Score1.ForeColor = vbBlack
End If

If dblScore2 <> [Score2] Then
Me.Score2.FontItalic = True
Me.Score2.ForeColor = vbRed
Else
Me.Score2.FontItalic = False
Me.Score2.ForeColor = vbBlack
End If

If dblScore3 <> [Score3] Then
Me.Score3.FontItalic = True
Me.Score3.ForeColor = vbRed
Else
Me.Score3.FontItalic = False
Me.Score3.ForeColor = vbBlack
End If
End If

lngProject = [Project]
dblScore1 = [Score1]
dblScore2 = [Score2]
dblScore3 = [Score3]

End Sub


However, I forgot to mention that some of the entries were originally blank (null) and there were some inputs on the later dates. While running with the codes, I received the following error:

"Run Time error '94':
Invalid Use of Null"

Can anyone assist? Thank you.
Tamrak
Hello,

Thank you. I am almost there.

Let's say that my data looks like the following:

1234, 12/05/2006, 1.5, 2.5, 3.5
1234, 12/08/2006, 1.5, , 3.5
1234, 12/10/2006, 1.7 , 2.7, 3.5
1800, 12/1/2006, 2.5, 3.5, 3.8
1800, 12/3/2006, , 3.7 , 3.4
1800, 12/6/2006, 2.7,3.7,

If the value changed from null to the number, the red italic fonts should be there as well. Can you assist? I still have the run time error '94' regarding invalid use of null.

Another thing is, if I add another text field and Dim that textfield as String, I should be able to continue using the same logic, am I correct?

For example:
Project,ActivityDate,Score1,Score2,Score3,changed_by


1234, 12/05/2006, 1.5, 2.5, 3.5, Paul
1234, 12/08/2006, 1.5, , 3.5, Paul
1234, 12/10/2006, 1.7 , 2.7, 3.5, Peter
1800, 12/1/2006, 2.5, 3.5, 3.8, Mary
1800, 12/3/2006, , 3.7 , 3.4, Meredith
1800, 12/6/2006, 2.7,3.7,Meredith

Thanks.
Tamrak
Hello again,

I am changing the codes to match BrianS above

I received an error "Method or Data Member Not Found."

The debug highlighted, "txtProjectNum", "txtScore1", "txtScore2", "txtScore3", "txtChanged_by" in

<.....>
If i = 1 Then

ProjectNum = Me.txtProjectNum
Score1 = Me.txtScore1
Score2 = Me.txtScore2
Score3 = Me.txtScore3
Changed_by = Me.txtChanged_By

End if

<...>

The properties for ProjectNum, Score1, Score2 and Score3 are numeric. The changed_by is string.

Can you let me know what I do wrong? Thanks.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.