VFP - Getting Number of Pages For a Group When Page Numbers Start at 1 Per Group

A question arose in the VFP forum concerning printing data within the Page Footer area that relates to the current group, rather than within the Group Footer.
I showed how to do this, but at the time could not address how to get the number of pages used for each group when page numbering is reset to 1 for each group. In other words how to obtain N for the normal "Page 1 of N" given at the  bottom of a page. Well now I have a way to do it. All within the report using an array and array subscript variable.

  1. Create a report variable - say RptArrSub - used to index the array to hold the last page number per group.
    Set "Initial Value" to 0 and "Value to Store" to RptArrSub. This retains its value when each record in the report cursor is processed.

  2. On the Reports Property window click the Other tab and then Edit settings within Run-time extensions.
    Put In Execute when: BeforeReport 
    and the following code into the script after the lparameters list.

** force a two pass process
toListener.TwoPassProcess = .T.

* when starting the report
IF toListener.currentpass = 0  then
   * release the array if it already exists
   RELEASE rptarry1
   * create the array as Public and set its intial value
   PUBLIC rptarry1[1]
   store 1 to rptarry1[1]
endif*

  1. In the Properties window of the Group Header Band, click the Other tab and then Edit Settings in Run-time extensions.
    Put in Execute When: BeforeBand
    Now in the Run-time extensions: code after the lparameters list put

** increment the array index variable
store (RptArrSub + 1) to RptArrSub

* increase the dimension of the array, but only on the first pass
IF toListener.currentpass = 0 and ALEN(rptarry1,1) < RptArrSub  then
   DIMENSION rptarry1[RptArrSub]
   store 1 to rptarry1[RptArrSub]
endif*

  1. In the Properties window of the Group Footer Band click the Other tab and then Edit Settings in Run-time extensions.  In Execute When: put BeforeBand
    Now in the Run-time extensions: code after the lparameters list put

* check it is the first run of a 2 pass print process
if toListener.currentpass = 0  then
   if ALEN(rptarry1,1) < RptArrSub then
    * not much we can do here
   else
     * save the current page number into the array
        store _pageno to rptarry1[RptArrSub]
   endif
endif

  1. Now add a Field to the report in the PageFooter and set its Expression to
    "Page " + ALLTRIM(STR(_PAGENO)) + " of " + iif(rptarrsub >0 and rptarrsub <= alen(rptarry1,1) , str(rptarry1[rptarrsub],3), "??")

Now when you require a report in which a data group restarts page numbering at 1, you do not need to lose the "Page 1 of N" element at the bottom of each page.