Access DoCmd print method - ms-access

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"

Related

Open Report if field contains certain word

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

Ms Access filter reports in runtime mode

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") & "#"

VBA Access strange behavior with global variables

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

Adding watermark to a Access report page

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.

VBA code is printing report instead of opening it

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