Access Do.Cmd OpenReport is only printing? - ms-access

Access 2010 - OpenReport in vba is only printing.
I have a simple modal form where the user selects a date range and the report opens. If the query results are 0, a message pops up saying there are no values, else the report opens and modal form closes. However, every time I run this it will not open in the report view it will only print. I can change it to design and print preview and those all work just not view.
I have been trying to figure this out with no avail and its driving me nuts. What am I missing?
Private Sub Command5_Click()
If DCount("*", "qryalltime_filtered") = 0 Then
MsgBox "No records to display based on the date parameter provided"
Else: DoCmd.OpenReport "rptAllTime", acViewReport
DoCmd.Close acForm, "frmAdmin-Employee"
End If
End Sub

If you wish to preview:
DoCmd.OpenReport "rptAllTime", acViewPreview

I just want to see it in the report view and not preview. I wrote the line again (like the 5th time today after restarting my machine) and I got this to work:
DoCmd.OpenReport "RptAllTime", acViewReport
I have no idea why it worked after trying so many times. Ugh, Microsoft....

I suggest to use this command :
DoCmd.OpenReport "rptAllTime", acViewReport, , , acWindowNormal

Related

Access report has to be refreshed to show data

I have a button that when pressed, opens a report on the record selected in that form. Here is the code for the button:
blnSave = MsgBox("Are you sure you want to exit this student?", vbYesNo, "Exit Confirmation")
If blnSave = True Then
DoCmd.OpenReport "rptExitNotice", acViewPreview, , "[BehaviourID]=" & Me.BehaviourID
DoCmd.Close acForm, "frmExitStudent"
End If
When the form closes and the report opens, all the fields in the report are blank:
When you press F5, the data appear perfectly in place:
I have tried putting both Me.Refresh, DoCmd.Requery and DoCmd.Refresh in the Open, No Data and Activate events, but they give these errors:
How can I get the report to show the data first time round?
Is there a problem with the button's code?
Or should I add a Me.Refresh or something similar in a different event?
I scrapped the code in the On Load, On Open, etc. and created a form with a timer of 1000 and the timer event executes this code:
Private Sub Form_Timer()
Dim rpt As Report
With Reports
' Iterate over all open reports...
For Each rpt In Reports
rpt.Requery
Next
End With
lblStatus.Caption = "Generation Complete"
DoCmd.Close acForm, "frmRefreshReport"
End Sub
The form refreshes the report and then closes. There is a little status label that changes text when the refresh is complete (to make it look nicer for end users). The form opens when the report opens, so the report gets refreshed by the form that then closes itself, saving the user pressisng F5.
Source: How to automatically reload a report in MS Access?

Access Print Report Options for Custom Form Printing

I have a custom form and report in Access and I have a 'Print Report' button on my form that takes the current record for the database and prints out the Report I created for it. My problem is the code I have does a quick print on the Report. Once I press the "Print report" button I created, it pops up the Print Preview but then automatically prints the report. I want to be able to look at the preview before the report prints, open the print dialog and be able to select which printer I want to use, instead of it going to Quick Print and using the default printer. My code is below for the current format, I'm not sure where or how to make this adjustment. Thanks in advance for any assistance!
Private Sub cmdPrintReport_Click()
Dim strReportName As String
Dim strCriteria As String
strReportName = "rptDrillReclamation"
strCriteria = "[HoleNumber]='" & Me![HoleNumber] & "'"
DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
On Error GoTo Err_cmdPrintReport_Click
Dim stDocName As String
stDocName = "rptDrillReclamation"
DoCmd.OpenReport stDocName, acNormal
Exit_cmdPrintReport_Click:
Exit Sub
Err_cmdPrintReport_Click:
MsgBox Err.Description
Resume Exit_cmdPrintReport_Click
End Sub
You are opening the report twice! Two lines of code starting with DoCmd.OpenReport.
The first time the View parameter is set to acViewPreview so it shows up (=previews) on the screen without printing. The second time the View parameter is set to acNormal (= no preview) so it prints directly to the printer without showing on the screen.
BTW The second time you are printing all the records because the criteria are not sent!
Remove the second Docmd.OpenReport.... It is unnecessary and is what is causing your problem.

Access Report not Filtering when Others Are

I have a form which allows the user to edit the properties of a filter via some combo boxes, then open a report. The report is opened with
DoCmd.OpenReport rptName, acViewReport, , whereClause, acWindowNormal
'whereClause = "Building = '005'" for instance
Some reports open fine, by which I mean they populate with the filtered info. Others, however, even though they are based off the same in-house template, IGNORE the filter all together and display a report based on ALL data (not the filtered data).
Why would a report ignore the filter? When I edit the reports in design mode after opening them with the form:
Working Report | Non Working Report
Filter: Building = '005' | Filter:
Filter On Load: No | Filter On Load: No
What could be causing the non-working report to not register the filter argument? There's no On Load VBA, nor any VBA, in these reports (except for an Export to PDF and a close button which is copy-paste for each report).
EDIT
Should have checked before, this happens regardless of whether or not the query driving the report is empty (ie the filter is never applied to some reports, regardless of blankness)
Not sure if the code will help, but:
Private Sub btnOpenSummary_Click()
If IsNull(Me.cboSummary) Then
MsgBox "Please select a building for the SUMMARY report."
Exit Sub
End If
strCrit = "Building = '" & Me.cboSummary & "'"
MsgBox strCrit
survArray = getSurveyArray()
For Each Survey In survArray
DoCmd.OpenReport Survey, acViewReport, , strCrit, acWindowNormal
Next Survey
DoCmd.OpenReport "Total Summary", acViewReport, , , , Me.cboSummary
End Sub
My fault.. there was code which had for some reason been linefed all the way down the code page and out of view. There was an On Open which played with the Control Source. It ended up being useless (as the control source only needed to be set once, not every time) but it was disabling the filter for some reason. Anybody else who may have this problem:
Make sure the control source is not being altered in your VBA for the report.
Thanks to those that helped, sorry my information was wrong.
My OnOpen code:
Private Sub Form_Open(Cancel as Integer)
Me.RecordSource = Me.Name
End Sub
It takes the name of the report, which corresponds to a name of a query, and puts it as the recordsource. (I have about 40 reports done this way, so it's dependent on names to make it fast to duplicate for different items).
Removing this made it work perfectly using Access 2010. Not sure if this was a problem specific to my setup or what, but, this change directly fixed it.
This is not a direct solution, but rather a workaround which should almost certainly work. Instead of applying filtering to the report, dynamically change the report data source by passing the where clause as a parameter.
To open the report use:
DoCmd.OpenReport rptName, acViewReport, , , acWindowNormal, whereClause
Note that the whereClause string is being passed as the OpenArgs parameter.
Then in the report VB:
Private Sub Report_Open(Cancel As Integer)
On Error GoTo ReportOpenError
If Not(IsNull(Me.OpenArgs)) Then
Me.RecordSource = Replace(Me.RecordSource,";"," WHERE " & Me.OpenArgs & ";")
End If
Exit Sub
ReportOpenError:
MsgBox "Unable to open the specified report"
Cancel = 1
End Sub
This solution assumes the report RecordSource is defined as a semicolon terminated SQL query (not a query name) and the record source does not already contain any WHERE, GROUP BY, etc., clauses. In those cases, it may be easier to redefine the query from scratch.
This problem may also occur when a report is copied and re-purposed. I had a report that was working fine then made a copy of it and was no longer able to filter it.
Perhaps there is a glitch in Access that causes it to ignore the filter when the report is copied under certain circumstances.
Solution: Instead of copying and re-naming the report, try creating a new report, linking the data source, and copying the fields back into place. If you are dealing with a new report, try re-creating it.

Running VBA code in MS Access report when it is openned without preview for printing

The below code prints a MS Access Report without opening it for preview.
However, I would like to run some code to modify the report when a user selects to print it.
DoCmd.OpenReport RptName, , , "[ItemNumber]= " & Me.ItemNum
I have tried the "On Activate", "On Open", and "On Page" events but none of them run the code that I place in there.
Each ItemNumber has an image associated with it. Whenever they hit the print button the above code runs sending the itemNumber they would like printed and at that moment I would like to insert the appropriate unbounded image to print on the report.
Put your code in the Format event of the Detail section. Assuming you have an image control named Image1 in the detail section of your report:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.Image1.Picture = DLookup("ImagePath", "ImageTable", _
"ItemNumber=" & Me.ItemNum)
End Sub

MS Access 2003 - Opening a report without it printing

I used
Docmd.OpenReport "Report1"
from another form and it seems to just want to print the report without actually displaying it. I want to display the report, not print it
Set the acView argument to acViewPreview:
docmd.OpenReport "Report1", acViewPreview
Access Help says you can use one of these AcView constants:
acViewDesign
acViewNormal
acViewPreview
acViewNormal is the default and prints the report immediately.