I have a form that calls a series of reports, one after the other. I also have a global variable that I use as a counter. Every time a page's PageFooterSection_Format is called, I want to increment the count. The problem is, the counter is counting higher that it should. Here is my code:
I have a module called genericFunctions:
Option Compare Database
Public pageCount As Integer
In my form, I have a loop that calls this:
'before loop
pageCount = 1
'start loop, which I left out for brevity
'run in previewView first so page footer format function is called
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
'then run this to open in report view, so onload event runs
DoCmd.OpenReport reportName, acViewReport, , , acHidden, !ID
'save report as a pdf
DoCmd.OutputTo acOutputReport, reportName, "PDF", rptPath
'close report
DoCmd.Close acReport, reportName
'I then have a method that "stitches" these individual reports into one PDF
now, in my report, I have this code:
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If (Report.CurrentView = 5) Then
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
End If
End Sub
I then place a breakpoint in the event. This event is hit 3 times, as it should. So, I would expect pgNumber text box to have 1,2,3 as their values but it has 2,4,6 instead. No where else in my code do I increment the pageCount variable. What's going on? Is this a scope issue?
PageFooterSection_Format runs both for DoCmd.OpenReport reportName, acViewPreview and for DoCmd.OutputTo acOutputReport, reportName, "PDF", because the latter is like a print command.
In both cases, Report.CurrentView = acViewReport (5). I'm not sure what to make of that - it feels like a bug. (Note: this is Access 2010)
Anyway, since DoCmd.OutputTo is like a printing (and thus like a print preview), you can simply omit this line:
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
At least that worked for me.
Note: Breakpoints in event procedures can be misleading as to what happens when. It is safer to use Debug.Print calls.
My test code was:
Sub ReportTesting()
Const reportName = "Bericht4"
'before loop
pageCount = 1
'run in previewView first so page footer format function is called
Debug.Print pageCount, "acViewPreview"
DoCmd.OpenReport reportName, acViewPreview, , , acHidden
'then run this to open in report view, so onload event runs
Debug.Print pageCount, "acViewReport"
DoCmd.OpenReport reportName, acViewReport, , , acHidden
'save report as a pdf
Debug.Print pageCount, "acOutputReport"
DoCmd.OutputTo acOutputReport, reportName, "PDF", "C:\test.pdf"
'close report
DoCmd.Close acReport, reportName
Debug.Print pageCount, "done"
End Sub
and
Private Sub Report_Open(Cancel As Integer)
Debug.Print "Report View: " & Me.CurrentView
End Sub
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If Me.CurrentView = acViewReport Then
Debug.Print "Footer - acViewReport"
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
Else
Debug.Print "Footer - other view" ' <-- this never happens
End If
End Sub
Output in Immediate window:
1 acViewPreview
Report View: 5
Footer - acViewReport
2 acViewReport
Report View: 6
2 acOutputReport
Footer - acViewReport
3 done
Related
We have a form with 2 buttons pointing to 2 reports, ideally we would like one button on the form to prevent user printing incorrect certificates
Is there a way to have only one button so when a user clicks on the button to run a report, that
if [type] contains "semi" open report "semi cert" & if [type] contains "spring" open report "spring cert"
these are our currents buttons
Private Sub semi_Click()
DoCmd.OpenReport "Quick Hitch (SEMI) Certificate", acViewPreview, , "[no] = " & Me.[no]
DoCmd.PrintOut
DoCmd.Close
End Sub
Private Sub spring_Click()
DoCmd.OpenReport "Quick Hitch (SPRING) Certificate", acViewPreview, , "[no] = " & Me.[no]
DoCmd.PrintOut
DoCmd.Close
End Sub
Assuming that type is the name of your text box
Private Sub OpenReport_Click()
Dim reportName As String
Select Case Me.[type].Value
Case "semi"
reportName = "Quick Hitch (SEMI) Certificate"
Case "spring"
reportName = "Quick Hitch (SPRING) Certificate"
Case Else
MsgBox "Report type '" & Me.[type].Value & "'is unknown"
Exit Sub
End Select
DoCmd.OpenReport reportName , acViewPreview, , "[no] = " & Me.[no]
DoCmd.PrintOut
DoCmd.Close
End Sub
How do you assign a name to a MS Access report created via CreateReport?
Code creates a dynamic report then adds controls to the report. Last, I want to name/save the report as "Student_Scores_Report01" but it appears that CreateReport auto-generates the name for the report as Report1, Report2, etc. I've run into issues where Access creates a "Report1" but it doesn't actually exist, causing the whole project to be corrupted. This is my best guess at working around this but it seems inefficient:
Dim rpt as Report
Set rpt = CreateReport
Dim ReptNmTemp as String
ReptNmTemp = rpt.name
DoCmd.Save acReport, rpt.Name
DoCmd.Close acReport, rpt.Name
DoCmd.Rename "Student_Scores_Report01", acReport, ReptNmTemp
For Each rpt02 In CurrentProject.AllReports
If rpt02.Name = ReptNmTemp Then
DoCmd.DeleteObject acReport, ReptNmTemp
End If
Next
The For...each loop deletes the auto-generated report and avoids the corruption problem. But is there a better way? Thanks in advance.
Instead of looping you could On Error Resume Next or some other error handler.
Dim rpt as Report
Dim ReptNmTemp as String
Set rpt = CreateReport
ReptNmTemp = rpt.name
DoCmd.Save acReport, ReptNmTemp
DoCmd.Close acReport, ReptNmTemp, acSaveYes
DoCmd.Rename "Student_Scores_Report01", acReport, ReptNmTemp
On Error Resume Next
DoCmd.DeleteObject acReport, ReptNmTemp
Or use acDefault, which is actually a default parameter for both Save and Close, which means if parameter is not provided, acDefault will be used.
Dim rpt as Report
Set rpt = CreateReport
DoCmd.Save , "Student_Scores_Report01"
DoCmd.Close , , acSaveYes
I have a report that displays another form's recordsource. When I add some record on this form and click for report, this records is not being displayed. How can I achieve displaying what is on screen - allways ? Here is my simple code for report:
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
End Sub
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = Forms![MyForm].Form.RecordSource
End Sub
You need to close the Report before re-opening it and save the new record.
In the click event of your Open Report button insert the following:
Private Sub OpenReport_Click()
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acReport, "Test1", acSaveYes
DoCmd.OpenReport "Test1", acViewPreview
End Sub
Try requerying your Forms Recordset before opening the report:
Private Sub cmdOpenReport_Click()
Me.FilterOn = False
Me.Requery
DoCmd.OpenReport "MyReport", acViewReport
End Sub
I have an answer, I knew It's not so easy (in click event of OpenReport button):
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Answer provided by Leigh Purvis (Access MVP), a BIG thanks once more !!
I'm selecting some record and I have option to view them as a report (by pushing Command33 button)
Private Sub Command33_Click()
DoCmd.OpenReport "repAwariaOtwarta", acViewPreview, , "[dbAwarieOtwarte].[ID] =" & Me![ID]
End Sub
After clicking the button I have report pop up.
What I should do to print that report without overwiewing it after clicking Command33?
I was trying to use DoCmd.PrintOut, but I seem to failed.
Just combine your ideas:
Open the report, use the print command.
Private Sub Command33_Click()
DoCmd.OpenReport "repAwariaOtwarta", acViewPreview, , _
"[dbAwarieOtwarte].[ID] =" & Me![ID]
DoCmd.PrintOut , , , , 1
End Sub
Here are the paramteres for Printout command
https://msdn.microsoft.com/en-us/library/office/ff192667.aspx
If you want, you can do a close method for the report after the print commmand like
DoCmd.Close acReport, "repAwariaOtwarta"
I'm trying to work out a problem and need some help. I have 3 types of users (T8XXXXX, P9XXXXX and XXXXX) each of these users have different elements and as such has a different report. The top two if statements work, as far as opening the reports, one report is blank and the third fails all together. I'm not really sure how to solve this, and am hoping for some help from professionals. Any help is greatly appreciated!
Private Sub cmdPrintIndRep_Click()
Dim strUser As String
strUser = Forms.frmsupresults.subfrmUsrRes!UserName
If Left(strUser, 2) Like "t8" Then
DoCmd.OpenReport "FedInvest - ISSR Recertification Report", acViewPreview
ElseIf Left(strUser, 2) Like "p9" Then
DoCmd.OpenReport "Courts - ISSR Recertification Report", acViewPreview
Else Left(strUser, 2) not Like "p9" and not like "t8"" Then
DoCmd.OpenReport "FedInvest - ISSR Internal Users Recertification Report", acViewPreview
End If
End Sub
Disclaimer: I'm no professional but hopefully I can help.
I adjusted the definition of strUser by using a reference to referring to controls found here.
Assuming there are only 3 types of users, I simply removed the last if statement. If it is not the 1st type, nor the 2nd type, it must be the 3rd type of user.
Lastly, we should use = instead of Like in the conditions for the if statements.
Private Sub cmdPrintIndRep_Click()
Dim strUser As String
strUser = Forms!frmsupresults!subfrmUsrRes.Form!UserName
If Left(strUser, 2) = "t8" Or Left(strUser, 2) = "T8" Then
DoCmd.OpenReport "FedInvest - ISSR Recertification Report", acViewPreview
ElseIf Left(strUser, 2) = "p9" Or Left(strUser, 2) = "P9" Then
DoCmd.OpenReport "Courts - ISSR Recertification Report", acViewPreview
Else
DoCmd.OpenReport "FedInvest - ISSR Internal Users Recertification Report", _
acViewPreview
End If
End Sub
As far as your reports being blank, what is their Record Source?