Jesse
09-28-2014, 11:15 AM
Hi Folks,
Disclaimer - I'm no expert in VBA nor Access so please excuse my ignorance and lack of proper coding terms.
Overview - I have a DB that creates a set of reports for an individual user, generates an email, and attaches a combined pdf with a summary of reports for that user. I have two users groups: Individual employees, and managers. For individual employees, my "combinePDF" code works to combine multiple reports into one attachment. As a new requirement, I'm looking to add the same functionality for all manager reports (2).
How it works(or supposed to)
pass three variables into the "combinePDF" function: Report to be sent, userID, and directory
Create individual objects for each applicable PDF report
Logic to evaluate which report is coming in: Employee reports OR Manager Reports
Create directory and destination to store PDF's reports, confirm the report should exist, output relevant reports to destination
The combined report is picked up from the destination with an email function and sent to the user
Problem - When re-creating the code to combine the manager reports into one PDF and store them in the destination folder for email extraction; the second report is not being sent. When checking if my report should exist(see snippet below) by running a query against my db, the line in red behaves different than expected. Specifically,instead of connecting to the DB , running the select statement, and moving to the next IF statement, it skips to a different step in another function and does not complete the "CombinePDF" function. Totally confused as to why since the individual report uses the same logic but does not have this issue.
Code Snippet
strSql = "SELECT * FROM qry_ManagerDirectsDevicesFiltered WHERE [ManagerID] = '" & sourceUserID & "';"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source4Name, acFormatPDF, directory & Source4Name & ".pdf", False
PDFSource4.Open (directory & Source4Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource4, 0, PDFSource4.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
Below is the full set of code for the CombinePDF function:
Function CombinePDF(reportToSend As Variant, sourceUserID As Variant, directory As String)
'Relies on the Adobe Acrobat 9.0 Type Library
Dim PDFDestination As Acrobat.AcroPDDoc
Dim PDFSource1 As Acrobat.AcroPDDoc
Dim PDFSource2 As Acrobat.AcroPDDoc
Dim PDFSource3 As Acrobat.AcroPDDoc
Dim PDFSource4 As Acrobat.AcroPDDoc
Dim DestinationName As String
Dim Source1Name As String
Dim Source2Name As String
Dim Source3Name As String
Dim Source4Name As String
'Initialize the objects
Set PDFDestination = CreateObject("AcroExch.PDDoc")
Set PDFSource1 = CreateObject("AcroExch.PDDoc")
Set PDFSource2 = CreateObject("AcroExch.PDDoc")
Set PDFSource3 = CreateObject("AcroExch.PDDoc")
Set PDFSource4 = CreateObject("AcroExch.PDDoc")
'NOTE: Source files are only applicable for Individual Report
' The manager report currently only uses a single report (trying to change this with the code below)
Dim rs As DAO.Recordset, qry As DAO.QueryDef
Dim db As DAO.Database
'Create and combine reports for individual report
If (reportToSend = "Rpt_Employees") Then
DestinationName = "rpt_Employees_Summary"
Source1Name = "rpt_Employees"
Source2Name = "rpt_Employees_Audio"
Source3Name = "rpt_EmployeeAssets"
'Open Destination, all other documents will be added to this and saved with
'a new filename
DoCmd.OutputTo acOutputReport, DestinationName, acFormatPDF, directory & DestinationName & ".pdf", False
PDFDestination.Open (directory & DestinationName & ".pdf")
'Determine if user has cellular devices; create and add cellular report
'NOTE: This is NOT the query used to populate the report; it just validates that the report should exist
Set db = CurrentDb
strSql = "SELECT * FROM qry_InvoiceLine WHERE sapUserID = '" & sourceUserID & "' AND DateOfInvoice BETWEEN #" & Forms!frm_Employees!StartDate & "# AND #" & Forms!frm_Employees!EndDate & "#;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
DoCmd.OutputTo acOutputReport, Source1Name, acFormatPDF, directory & Source1Name & ".pdf", False
PDFSource1.Open (directory & Source1Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource1, 0, PDFSource1.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource1.Close
End If
rs.Close
'Determine if user has audio conferencing charges; create and add audio conferencing report
strSql = "SELECT * FROM ATT_Audio_Domestic WHERE [Enterprise ID] = '" & sourceUserID & "' AND InvoiceMonth BETWEEN #" & Forms!frm_Employees!StartDate & "# AND #" & Forms!frm_Employees!EndDate & "#;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source2Name, acFormatPDF, directory & Source2Name & ".pdf", False
PDFSource2.Open (directory & Source2Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource2, 0, PDFSource2.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource2.Close
'Loop
End If
rs.Close
'Determine if the user has assets; create and add Employee Assets Report
strSql = "SELECT * FROM Assets WHERE [UserID] = '" & sourceUserID & "' ;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source3Name, acFormatPDF, directory & Source3Name & ".pdf", False
PDFSource3.Open (directory & Source3Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource3, 0, PDFSource3.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource3.Close
'Loop
End If
rs.Close
ElseIf (reportToSend = "rpt_DirectReports") Then
DestinationName = "rpt_DirectReports"
Source4Name = "rpt_ManagerDirectsAssets"
'Open Destination, all other documents will be added to this and saved with
'a new filename
DoCmd.OutputTo acOutputReport, DestinationName, acFormatPDF, directory & DestinationName & ".pdf", False
PDFDestination.Open (directory & DestinationName & ".pdf")
'Determine if the users directs have assets(pc, tablet, network card, servers, etc..)
'; create and add Manger Directs device report
Set db = CurrentDb
strSql = "SELECT * FROM qry_ManagerDirectsDevicesFiltered WHERE [ManagerID] = '" & sourceUserID & "';"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source4Name, acFormatPDF, directory & Source4Name & ".pdf", False
PDFSource4.Open (directory & Source4Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource4, 0, PDFSource4.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource4.Close
'Loop
End If
rs1.Close
End If
'Save and close the file
'This is run for the manager directs report
PDFDestination.Save PDSaveFull, directory & reportToSend & ".pdf"
PDFDestination.Close
'Return the PDFDestination and reset variables
CombinePDF = PDFDestination
Set PDFSource1 = Nothing
Set PDFSource2 = Nothing
Set PDFSource3 = Nothing
Set PDFSource4 = Nothing
Set PDFDestination = Nothing
Disclaimer - I'm no expert in VBA nor Access so please excuse my ignorance and lack of proper coding terms.
Overview - I have a DB that creates a set of reports for an individual user, generates an email, and attaches a combined pdf with a summary of reports for that user. I have two users groups: Individual employees, and managers. For individual employees, my "combinePDF" code works to combine multiple reports into one attachment. As a new requirement, I'm looking to add the same functionality for all manager reports (2).
How it works(or supposed to)
pass three variables into the "combinePDF" function: Report to be sent, userID, and directory
Create individual objects for each applicable PDF report
Logic to evaluate which report is coming in: Employee reports OR Manager Reports
Create directory and destination to store PDF's reports, confirm the report should exist, output relevant reports to destination
The combined report is picked up from the destination with an email function and sent to the user
Problem - When re-creating the code to combine the manager reports into one PDF and store them in the destination folder for email extraction; the second report is not being sent. When checking if my report should exist(see snippet below) by running a query against my db, the line in red behaves different than expected. Specifically,instead of connecting to the DB , running the select statement, and moving to the next IF statement, it skips to a different step in another function and does not complete the "CombinePDF" function. Totally confused as to why since the individual report uses the same logic but does not have this issue.
Code Snippet
strSql = "SELECT * FROM qry_ManagerDirectsDevicesFiltered WHERE [ManagerID] = '" & sourceUserID & "';"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source4Name, acFormatPDF, directory & Source4Name & ".pdf", False
PDFSource4.Open (directory & Source4Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource4, 0, PDFSource4.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
Below is the full set of code for the CombinePDF function:
Function CombinePDF(reportToSend As Variant, sourceUserID As Variant, directory As String)
'Relies on the Adobe Acrobat 9.0 Type Library
Dim PDFDestination As Acrobat.AcroPDDoc
Dim PDFSource1 As Acrobat.AcroPDDoc
Dim PDFSource2 As Acrobat.AcroPDDoc
Dim PDFSource3 As Acrobat.AcroPDDoc
Dim PDFSource4 As Acrobat.AcroPDDoc
Dim DestinationName As String
Dim Source1Name As String
Dim Source2Name As String
Dim Source3Name As String
Dim Source4Name As String
'Initialize the objects
Set PDFDestination = CreateObject("AcroExch.PDDoc")
Set PDFSource1 = CreateObject("AcroExch.PDDoc")
Set PDFSource2 = CreateObject("AcroExch.PDDoc")
Set PDFSource3 = CreateObject("AcroExch.PDDoc")
Set PDFSource4 = CreateObject("AcroExch.PDDoc")
'NOTE: Source files are only applicable for Individual Report
' The manager report currently only uses a single report (trying to change this with the code below)
Dim rs As DAO.Recordset, qry As DAO.QueryDef
Dim db As DAO.Database
'Create and combine reports for individual report
If (reportToSend = "Rpt_Employees") Then
DestinationName = "rpt_Employees_Summary"
Source1Name = "rpt_Employees"
Source2Name = "rpt_Employees_Audio"
Source3Name = "rpt_EmployeeAssets"
'Open Destination, all other documents will be added to this and saved with
'a new filename
DoCmd.OutputTo acOutputReport, DestinationName, acFormatPDF, directory & DestinationName & ".pdf", False
PDFDestination.Open (directory & DestinationName & ".pdf")
'Determine if user has cellular devices; create and add cellular report
'NOTE: This is NOT the query used to populate the report; it just validates that the report should exist
Set db = CurrentDb
strSql = "SELECT * FROM qry_InvoiceLine WHERE sapUserID = '" & sourceUserID & "' AND DateOfInvoice BETWEEN #" & Forms!frm_Employees!StartDate & "# AND #" & Forms!frm_Employees!EndDate & "#;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
DoCmd.OutputTo acOutputReport, Source1Name, acFormatPDF, directory & Source1Name & ".pdf", False
PDFSource1.Open (directory & Source1Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource1, 0, PDFSource1.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource1.Close
End If
rs.Close
'Determine if user has audio conferencing charges; create and add audio conferencing report
strSql = "SELECT * FROM ATT_Audio_Domestic WHERE [Enterprise ID] = '" & sourceUserID & "' AND InvoiceMonth BETWEEN #" & Forms!frm_Employees!StartDate & "# AND #" & Forms!frm_Employees!EndDate & "#;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source2Name, acFormatPDF, directory & Source2Name & ".pdf", False
PDFSource2.Open (directory & Source2Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource2, 0, PDFSource2.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource2.Close
'Loop
End If
rs.Close
'Determine if the user has assets; create and add Employee Assets Report
strSql = "SELECT * FROM Assets WHERE [UserID] = '" & sourceUserID & "' ;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source3Name, acFormatPDF, directory & Source3Name & ".pdf", False
PDFSource3.Open (directory & Source3Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource3, 0, PDFSource3.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource3.Close
'Loop
End If
rs.Close
ElseIf (reportToSend = "rpt_DirectReports") Then
DestinationName = "rpt_DirectReports"
Source4Name = "rpt_ManagerDirectsAssets"
'Open Destination, all other documents will be added to this and saved with
'a new filename
DoCmd.OutputTo acOutputReport, DestinationName, acFormatPDF, directory & DestinationName & ".pdf", False
PDFDestination.Open (directory & DestinationName & ".pdf")
'Determine if the users directs have assets(pc, tablet, network card, servers, etc..)
'; create and add Manger Directs device report
Set db = CurrentDb
strSql = "SELECT * FROM qry_ManagerDirectsDevicesFiltered WHERE [ManagerID] = '" & sourceUserID & "';"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot, dbReadOnly)
If rs.RecordCount > 0 Then
'Do
'Open the source document that will be added to the destination
DoCmd.OutputTo acOutputReport, Source4Name, acFormatPDF, directory & Source4Name & ".pdf", False
PDFSource4.Open (directory & Source4Name & ".pdf")
If PDFDestination.InsertPages(PDFDestination.GetNumPages - 1, PDFSource4, 0, PDFSource4.GetNumPages, 0) Then
'-1 Success
Else
'0 problem
End If
PDFSource4.Close
'Loop
End If
rs1.Close
End If
'Save and close the file
'This is run for the manager directs report
PDFDestination.Save PDSaveFull, directory & reportToSend & ".pdf"
PDFDestination.Close
'Return the PDFDestination and reset variables
CombinePDF = PDFDestination
Set PDFSource1 = Nothing
Set PDFSource2 = Nothing
Set PDFSource3 = Nothing
Set PDFSource4 = Nothing
Set PDFDestination = Nothing