Hi All,
I'm very new to VBA but have cobbled together the following code from a number of more experienced users online. I have a report called ITD Summary (Division) that has page breaks for each different BFR Name, and I'm trying to use this code to export each of these BFR Name sections of the report as individual .pdf files. The code below executes just fine, but it ends up exporting the entire report for each of the individual BFR Names rather than just the individual section of the report. Any advice? Thanks in advance!
CODE (sorry for the poor formatting)
Sub TestModule()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim baseSQL As String
Dim rptSQL As String
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("SELECT [BFR Name] FROM [BFR Names]",
dbOpenSnapshot)
Set qdf = dbs.QueryDefs("[DOM Query 2]")
baseSQL = " SELECT [DOM Query 1].Investigator, [DOM Query 1].[Fund
Code], [DOM Query 1].Revenue, [DOM Query 1].[Non Salary Exp], [DOM
Query 1].[Salary Expense], [DOM Query 1].[Earned Rev], nz([DOM Query
1]![Non Salary Exp],0)+nz([DOM Query 1]![Salary Expense],0) AS [Total
Expenses], nz([DOM Query 1]![Revenue],0)+nz([DOM Query 1]![Non Salary
Exp],0)+nz([DOM Query 1]![Salary Expense],0) AS [R/3 Balance],
IIf(nz([Earned Rev],0)-nz([Revenue],0)<=0,nz([Earned
Rev],0)-nz([Revenue],0)) AS [A/R], nz([R/3 Balance],0)+nz([A/R],0) AS
[Adjusted Balance], [DOM Query 1].[Divisions], [DOM Query 1].[Monthly
Earned Rev], [DOM Query 1].[Fund Code & Name]" & _
" FROM [DOM Query 1]"
With rst
Do Until .EOF
rptSQL = baseSQL & " And [BFR Name] = " & ![BFR Name]
qdfSQL = rptSQL
DoCmd.OutputTo acOutputReport, "ITD Summary (Division)",
acFormatPDF, "C:\Documents and Settings\dms28\Desktop\" &
![BFR Name] & ".pdf"
.MoveNext
Loop
.Close
End With
qdf.SQL = baseSQL
Set qdf = Nothing
Set rst = Nothing
Set dbs = Nothing
End Sub
I'm very new to VBA but have cobbled together the following code from a number of more experienced users online. I have a report called ITD Summary (Division) that has page breaks for each different BFR Name, and I'm trying to use this code to export each of these BFR Name sections of the report as individual .pdf files. The code below executes just fine, but it ends up exporting the entire report for each of the individual BFR Names rather than just the individual section of the report. Any advice? Thanks in advance!
CODE (sorry for the poor formatting)
Sub TestModule()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim baseSQL As String
Dim rptSQL As String
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("SELECT [BFR Name] FROM [BFR Names]",
dbOpenSnapshot)
Set qdf = dbs.QueryDefs("[DOM Query 2]")
baseSQL = " SELECT [DOM Query 1].Investigator, [DOM Query 1].[Fund
Code], [DOM Query 1].Revenue, [DOM Query 1].[Non Salary Exp], [DOM
Query 1].[Salary Expense], [DOM Query 1].[Earned Rev], nz([DOM Query
1]![Non Salary Exp],0)+nz([DOM Query 1]![Salary Expense],0) AS [Total
Expenses], nz([DOM Query 1]![Revenue],0)+nz([DOM Query 1]![Non Salary
Exp],0)+nz([DOM Query 1]![Salary Expense],0) AS [R/3 Balance],
IIf(nz([Earned Rev],0)-nz([Revenue],0)<=0,nz([Earned
Rev],0)-nz([Revenue],0)) AS [A/R], nz([R/3 Balance],0)+nz([A/R],0) AS
[Adjusted Balance], [DOM Query 1].[Divisions], [DOM Query 1].[Monthly
Earned Rev], [DOM Query 1].[Fund Code & Name]" & _
" FROM [DOM Query 1]"
With rst
Do Until .EOF
rptSQL = baseSQL & " And [BFR Name] = " & ![BFR Name]
qdfSQL = rptSQL
DoCmd.OutputTo acOutputReport, "ITD Summary (Division)",
acFormatPDF, "C:\Documents and Settings\dms28\Desktop\" &
![BFR Name] & ".pdf"
.MoveNext
Loop
.Close
End With
qdf.SQL = baseSQL
Set qdf = Nothing
Set rst = Nothing
Set dbs = Nothing
End Sub