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.
Related
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
I've come across a challenging problem with the DoCmd.OutputTo command in VBA Access 2013.
I've got below code which is basically to print a specific report from what is essentially a collection of records of reimbursements. The idea is that one would be able to [export] the active record (from the active form) to a PDF file and then add the scanned invoices to the PDF.
Thereto I would need to build the base file (i.e. the PDF where the invocies are added to) and then run the routine for adding the individual files.
Below code should create the initial PDF file:
Dim rpt as Report
filePath = "<some filepath>"
fName = Me!idDecl & " - (script).pdf"
filePath = filePath & fName
Set rpt = Report_qryDeclInvoice
With rpt
.Filter = "[fltID]= " & Me!id
.FilterOn = True
End With
DoCmd.OpenReport rpt.Name, acViewPreview, , "[fltID]= " & Me!id
DoCmd.OutputTo acOutputReport, rpt.Name, acFormatPDF, filePath, False
DoCmd.Close acReport, "qryDeclInvoice"
If I run the code -without breaks- the report opens as per the filtered argument, however, the subsequent command to output the record to PDF doesn't ?
That is, there appears a dialog box very briefly (can't read what it says) and then execution simply stops, no errors, no faultcodes just a clean break ?
Now for the interesting bit..
If I set a breakpoint on the DoCmd.OutputTo line, and execute the line with F8 the code works more or less flawlessly (see below)?? It appears that the break allows the preview routine to complete first and then run the OutputTo routine.
In addition to above challenge, on some of the reports (i.e. on some reimbursements) it works fine and the file is created yet on others it does not create the initial PDF at all and the code breaks without error codes or fault reporting. Without there being a distinguishable difference between the reports ?
I've tried delaying the OutputTo from the OpenReport function by sleeping it for 1000ms but that doesn't work (even upto 5000ms doesn't yield results)
Also if I remove the open preview line and just execute the OutputTo line, without opening the preview first, it works only when breaking and executing with F8 and again, only on some of the reports not all ?
It seems that the OutputTo command is -at least in my case- somewhat unreliable :-)
Any suggestions ??
OK, found out what was going on;
The faulty reports, that I mentioned, created multiple pages, based on grouping.
A section of VBA code in/on the report, re-created page numbers based on grouping (i.e. restarted page numbering for start of every group.) The execution of this VBA within the report (i.e. on open, or on page) interferes with the execution of the "OutputTo" routine. The above interfered with the OutputTo routine, causing it to break, though not sure why -yet-
Removal of all VBA code within the report solved the issue !!
Some added details:
No need for the preview anymore
set the filter of the report from VBA and open the report through the report object.name
Find below the working code:
Dim rpt as Report
filePath = "<somepath>"
fName = Me!idDecl & "-(script).pdf"
filePath = filePath & fName
'Debug.Print filePath
Set rpt = Report_qryInvoice
With rpt
.Filter = "[fltID]= " & Me!id
.FilterOn = True
End With
'Sleep (5000)
DoCmd.OutputTo acOutputReport, rpt.Name, acFormatPDF, filePath, False, , , acExportQualityScreen
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.
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.
I'm working up an MS Access report. This report is grouped and has a details section. But some of these details rows don't need to be shown. How do I hide the details sections if, for example, "[fieldx] = 'foo'"?
(In Crystal Reports, there's a place where you can write custom criteria to suppress these kinds of things. I can't find an equivalent functionality here in Access).
As David suggested, you could pass WHERE condition to the report:
Function OpenReport(filterValue As String)
DoCmd.Close acReport, "ReportName"
DoCmd.OpenReport "ReportName", acViewReport, "", "[SomeFieldName]<>'" & filterValue & "'", acNormal
End Function