Avatar of AronMcD
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" twice...so 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
    Else
        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?  

Thanks!
Microsoft DevelopmentMicrosoft Access

Avatar of undefined
Last Comment
IrogSinta

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Dettman (EE MVE)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
IrogSinta

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...
AronMcD

ASKER
Thank you everyone!  I will read those articles cap..thanks.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Jim Dettman (EE MVE)

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.

Jim.
IrogSinta

Thanks for clearing that up, Jim.

Ron