Link to home
Start Free TrialLog in
Avatar of AronMcD

asked on

MS Access Report first record on new page being counted twice.

I have a report that I'm using to determine on-time delivery percentage.  What I'm doing is subtracting the Required by Date by the Actual Finish date.  I then have an icount variable that i increment by 1 when the subtraction listed above equals zero or higher (indicating the trailer was delivered on time).  The problem I'm having is that for some reason the first record on a new page is being "looked at" if the subtraction is zero or greater it's being counted twice.  This is obviously affecting my percentage calculation.  Here is my code (in the detail section)

Option Compare Database
Option Explicit

Private iCount As Integer

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    If Nz(Me.txtMustHave.Value, 0) = 0 Or Nz(Me.txtActualFinDate.Value, 0) = 0 Then
        Me.txtPromiseDelivery.Value = Null
        Me.txtPromiseDelivery.Value = DateDiff("d", Nz([FinCompleteDate], 0), Nz([RequiredBy], 0))
    End If

    If Me.txtPromiseDelivery.Value >= 0 Then
        iCount = iCount + 1            
    End If    

End Sub

Private Function getOTDCount() As Double
    getOTDCount = iCount
End Function

Open in new window

In the Report Footer section of my report I have a text box doing the % calculation with the control source set to: =getOTDCount()/Count([JobNo])

Has anyone run into this problem before?  How can I fix this?  Or is there a better way to do what I'm attempting to do here?  

Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you have Page of Pages at the footer of your report, the report will force the Format event to fire twice.  You can delete this if you don't need it.

You could also put your code in the OnPrint event instead which will only fire once even with a page count in your report.

Or you could add this to the beginning of your code in the OnFormat event:
   Static z As Integer
   If z > 0 Then Exit Sub Else z = z + 1
   'rest of code goes here...
Avatar of AronMcD


Thank you everyone!  I will read those articles cap..thanks.
Just to clear up what IrogSinta said:

"If you have Page of Pages at the footer of your report, the report will force the Format event to fire twice.  You can delete this if you don't need it."

 This is something different, which is two-pass printing.  If you reference the pages property in your report, the report engine will do two passes through the entire report.

 In the first pass, it runs through the report, but does not fire the OnPrint event.  It does this to get the total number of pages.

 Then on the second pass, it fires everything and actually prints the report.

 This is an important point because it means that if you do reference pages, you need to make sure that you init your totals in the report header event, or you will see things double up.

 This is different and seperate from a section repeating because of formatting, which is what I was pointing out.

  For example, you set KeepTogether True for a footer.   Report engine runs down the page, discovers that it doesn't have room for the footer, so it retreats up the page, doesn't print all the detail and then moves to a new page and continues printing.

Thanks for clearing that up, Jim.