470,612 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

Multiple page printing with nested loops

Hi guys,
thanks I am fairly new to this forum so I hope I chose the right place to post this question.

I try to make my program printout a deposit's report. I created a class to store the printing printing data and the actual database data in a recordset.
the class looks like this:
Expand|Select|Wrap|Line Numbers
  1. Public Class BRPDD 'Balance Report PrintingDocument Data
  3.         Public totalReceiptsNum As Integer
  4.         Public totalLicFee As Double
  5.         Public totalClerk As Double
  6.         Public totalTotalFees As Double
  7.         Public totalCash As Double
  8.         Public totalCheck As Double
  9.         Public totalChange As Double
  10.         Public ReceiptTotalFees As Double
  11.         Public FinalMark As String
  12.         Public DepositRS As DAO.Recordset
  13.         Public ReceiptRS As DAO.Recordset
  15.         Public Sub New()
  16.             ResetData()
  17.         End Sub
  19.         Public Sub ResetData()
  20.             totalReceiptsNum = 0
  21.             totalLicFee = 0D
  22.             totalClerk = 0D
  23.             totalTotalFees = 0D
  24.             totalCash = 0D
  25.             totalCheck = 0D
  26.             totalChange = 0D
  27.             ReceiptTotalFees = 0D
  28.             FinalMark = ""
  29.             DepositRS = Nothing
  30.             ReceiptRS = Nothing
  31.         End Sub
  32.     End Class
I made the class so that the current iteration of the receipts is not lost when the PrintPage handler sub is called again by
Expand|Select|Wrap|Line Numbers
  1. e.HasMorePages = True
So what I did is, I have a a form variable that is an instance of the class above, it is then initiated in the BeginPrint handler (DB class is a wrapper class to make easy query, I have tested this before and works fine, so the problem is not there):
Expand|Select|Wrap|Line Numbers
  1. Me.BalanceReport = New DBM.BRPDD
  2.         Dim dbobj As DBM.DB = New DBM.DB()
  5.         dbobj.openConnection()
  6.         dbobj.query("SELECT Deposits.* FROM Deposits WHERE DepositID=" & DepositIDString)
  7.         Me.BalanceReport.DepositRS = dbobj.Rs.Clone()
  9.         dbobj.query("SELECT Receipts.* FROM Receipts WHERE DepositID=" & DepositIDString)
  10.         Me.BalanceReport.ReceiptRS = dbobj.Rs.Clone()
  12.         Debug.Print("Report printing: initializing RSs.")
  13.         Debug.Print(Me.BalanceReport.totalCash.ToString())
  14.         Debug.Print(Me.BalanceReport.DepositRS.Fields.Count.ToString())
  15.         Debug.Print(Me.BalanceReport.ReceiptRS.Fields.Count.ToString())
  17.         dbobj.closeConnections()
  18.         dbobj = Nothing
The debug prints in that precedure they do return the right values. But when I go and put the same debug prints in the PrintPage says that the DepositRS and ReceiptsRS are "Object invalid or no longer set."

This is my PrintPage procedure:
Expand|Select|Wrap|Line Numbers
  1. Private Sub docuemt_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  2. Handles ReportPrintDocument.PrintPage        
  3.     Me.PrintSettings = New PrintingDocData(e)
  4.     Me.PrintSettings.LeftMargin = e.MarginBounds.Left - 50
  5.     Debug.Print("Report printing: inside sub.")
  6.     Debug.Print(Me.BalanceReport.totalCash.ToString())
  7.     Debug.Print(Me.BalanceReport.DepositRS.Fields.Count.ToString())
  8.     Debug.Print(Me.BalanceReport.ReceiptRS.Fields.Count.ToString())
  11.     Dim dbobj As DBM.DB = New DBM.DB()
  12.     dbobj.openConnection()
  14.     ' change paper orientation
  15.     ReportPrintDocument.DefaultPageSettings.Landscape = True
  17.     '' Printout header
  19.     '' loop through the receipts
  20.     Do Until BalanceReport.ReceiptRS.EOF
  21.         dbobj.query("SELECT LicenseFees.* FROM LicenseFees WHERE ReceiptID=" _
  22.                      & Me.BalanceReport.ReceiptRS.Fields("ReceiptID").Value.ToString())
  23.         Debug.Print("Report printing: selecting the licensefees.")
  24.         Do Until dbobj.Rs.EOF
  25.             '' printout the licenses that are in the receipt which takes only
  26.             '' 1 line of space.
  28.             'If more lines exist, print another page.
  29.             If Me.PrintSettings.CurHeight >= Me.PrintSettings.PageHeight Then
  30.                 e.HasMorePages = True
  31.                 Debug.Print("trying to add a second page in line 458.")
  32.                 Me.PrintSettings.CurPage += 1
  33.                 Exit Sub
  35.             Else
  36.                 e.HasMorePages = False
  37.             End If
  38.         Loop
  39.         Debug.Print("Report printing: printing receipts totals.")
  41.         'print the totals of current receipt
  43.         Debug.Print("Report printing: updating data variables.")
  44.         'Add the values to the totals
  45.         Me.BalanceReport.totalClerk += Double.Parse(Me.BalanceReport.ReceiptRS.Fields("ClerkFees").Value.ToString())
  46.         Me.BalanceReport.totalTotalFees += Double.Parse(Me.BalanceReport.ReceiptRS.Fields("Total").Value.ToString())
  47.         Me.BalanceReport.totalReceiptsNum += 1
  48.         'reset the current fee to 
  49.         Me.BalanceReport.ReceiptTotalFees = 0D
  50.         'move to the next record
  51.         Me.BalanceReport.ReceiptRS.MoveNext()
  52.     Loop
  54.     ' print totals deposit's totals
  56.     dbobj.closeConnections()
  57.     dbobj = Nothing
  58. End Sub
The error only happens with the DepositRS and ReceiptRS properties of the class, the other data in BalanceReport I can access it. A curious think is that BalanceReport is declared as Private on the form class, and when i change it to static or public it gives me an error saying "Error 1 'BalanceReport' cannot expose type 'DBM.BRPDD' outside the project through class 'DepositViewForm'."

I am thinking maybe after the BeginPrint something de-references these recordsets.

I don't know if any of you have an idea why this is happening or if there is a better way to printout multiple pages.

BTW I know DAO is old school, but I am using it because this old bug was just put in my hands and the department needs a quick fix for this. DAO recordsets are used throughout the muilti-form program so it is overhead to make the change to ADO.NET. I am going to advise to change it after this bug is fix.

Sorry for the long post, but really thank you in advance.
May 4 '09 #1
0 2603

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

25 posts views Thread by chad | last post: by
11 posts views Thread by Alban Hertroys | last post: by
46 posts views Thread by Neptune | last post: by
9 posts views Thread by Gregory Petrosyan | last post: by
4 posts views Thread by Matt Ratliff | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
5 posts views Thread by TP | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.