I have a button on a form that should open a report but it sending the report to the default printer instead. How do stop it from printing? I'm using access 2010
Private Sub Generate_Click()
If Me.Staff = "<< All >>" Then
DoCmd.OpenReport "rptAll", acViewNormal, , , acHidden
Else
DoCmd.OpenReport "rptStaff", acViewNormal, , , acHidden
End If
End Sub
You should use acViewReport as stated here in the MSDN doc if you want to show the report.
http://msdn.microsoft.com/en-us/library/office/ff195735%28v=office.15%29.aspx
Related
Does anyone knows how to filter reports the right way within Access runtime mode?
The usual code with DoCmd doesn't work.
This is what I tried for the report:
Private Sub Befehl217_Click()
DoCmd.OpenReport "Tagesbericht", acViewPreview
End Sub
Private Sub Bezeichnungsfeld26_Click()
DoCmd.GoToControl "DateFilter"
DoCmd.RunCommand acCmdFilterMenu
End Sub
This didn't work. Access complained that "FilterMenu isn't available".
I tried to create a context menu but this only displayed me cut, copy and paste.
You confirmed your report includes a control named Bezeichnungsfeld26 and when the user clicks in that control, you want to bring up the filter menu for that control.
When the user clicks in that control, it has the focus, so there is no need for GoToControl. And you don't want to go to a different control if you want the user to filter on Bezeichnungsfeld26.
Disable the GoToControl line ...
Private Sub Bezeichnungsfeld26_Click()
'DoCmd.GoToControl "DateFilter"
DoCmd.RunCommand acCmdFilterMenu
End Sub
You can use the Filter property:
Me.Filter = "[YourField] = " & somevalue & ""
Me.FilterOn = True
or, to expand on your current method:
DoCmd.OpenReport "Tagesbericht", acViewPreview, , "[YourField] = " & somevalue & ""
If you filter for a date you must pass a properly formatted string expression for the date:
Dim FilterDate As Date
FilterDate = Date
DoCmd.OpenReport "Tagesbericht", acViewPreview, , "[DateFilter] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#"
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
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 am trying to add a watermark to an Access report using the "Picture" property and have run into an issue. The following code works and the image is displayed/printed when the report is previewed and printed but does not work when the report is directly printed from a macro (nothing visible on-screen). "GrandTotal" is a bound text box on the report that is the sum of a field in the record source. I would appreciate any suggestion to print the watermark from both the print preview and the print macro.
Private Sub Report_Load
' put up the watermark if needed
If GrandTotal.Value < 2000 Then
Me.Picture = <<picture file name including full path>>
End If
End Sub
Since you are printing the report without it ever rendering to the screen the open/load events are never fired because they are never used. An alternative could be to open the report in print preview and use the OpenArgs to indicate you want to print it
Private Sub SomeButton_Click()
DoCmd.OpenReport "DetailView", acViewPreview, , , acHidden, "Print"
End Sub
then do your normal loading stuff
Private Sub Report_Load
' put up the watermark if needed
If GrandTotal.Value < 2000 Then
Me.Picture = <<picture file name including full path>>
End If
End Sub
and when the loading is done your form will Activate which is when you can print
Private Sub Report_Activate()
If Me.OpenArgs = "Print" Then
On Error GoTo ErrorHandler
DoCmd.OpenReport "Report1", acViewPreview
'Opens print dialog for current screen (report in this case):
DoCmd.RunCommand acCmdPrint
End If
DoCmd.Close
ErrorHandler:
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
Exit Sub
End If
End Sub
The form is never shown so it looks like you had it set up before but the load/open events will fire like normal because the report is actually rendered.
I'm using VBA to dynamically load the content of a report, and depending on which report is selected from the control panel form I've built, the report's query might be filtered.
At the beginning of my Report_Open function, I have this:
Private Sub Report_Open(Cancel As Integer)
Me.Filter = "VIP=True"
Me.FilterOnLoad = True
Now, this was working when I started this project - I had commented out these lines, and when uncommenting them discovered that this doesn't work properly anymore. Instead of the filter being applied when the report is loaded, I have to reload the report for the filter to work - either by switching to print preview and switching back to report view, or by switching to design view and then back to report view (in design view, the selected filter does display in the properties pane).
I am loading the report using command buttons that allow the user to view, export to PDF, or print (opens in print preview). None of these commands cause the report to open with the filter applied - it has to be reloaded.
The commands I'm using to load the report are below for reference:
If (Action = "View") Then
DoCmd.OpenReport "Test", acViewReport
ElseIf (Action = "PDF") Then
DoCmd.OutputTo acOutputReport, "Test", acFormatPDF
ElseIf (Action = "Print") Then
DoCmd.OpenReport "Test", acViewPreview
End If
Ok, I have no idea why Me.Filter and Me.FilterOnLoad don't work, since from everything I have seen on MSDN and elsewhere, it should work. That being said, I figured out that I can use DoCmd.ApplyFilter instead:
'Check if VIP - add filter if necessary
If (getGlobal(1) = True) Then
DoCmd.ApplyFilter , "VIP = True"
End If
I'd still like to know why the other way was behaving so oddly, if anyone has any ideas...
As #David-W-Fenton suggested, use the WhereCondition with OpenReport instead of setting a Filter expression. Your WhereCondition can be the same string you were using for the Filter expression.
Also, if you give OpenReport an empty string as the WhereCondition, the effect is the same as no WhereCondition, so this (untested) code should work whether or not your getGlobal(1) returns True.
Dim strWhereCondition As String
Dim strReport As String
strReport = "Test"
If (getGlobal(1) = True) Then
strWhereCondition = "VIP = True"
End If
Select Case Action
Case "View"
DoCmd.OpenReport strReport, acViewReport, , strWhereCondition
Case "PDF"
DoCmd.OpenReport strReport, acViewReport, , strWhereCondition
DoCmd.OutputTo acOutputReport, , acFormatPDF
Case "Print"
DoCmd.OpenReport strReport, acViewPreview, , strWhereCondition
End Select
Notice also that DoCmd.OutputTo, without an ObjectName argument, uses the active object ... which will be the "Test" report in this case.