Since you are using a database query, there may be a way to know how many pages you need by using the subreccount function. By knowing how many entries fit on a page and the number of records for a customer, you can determine the number of pages needed.
As an example, say you are printing invoices. The query would be set to separate pages when the client changes, so that all items purchased by the customer are in the datapage. You can print 15 items per page, so 50 items would need (50 / 15) = 3.25 thus will need 4 pages.
The document would contain all 30 pages, with the 28th page using the overflow feature.
Hope this helps