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.
Related
Pretty new at this. I have a main form with a list box of job numbers and a subform for the different reports. I want to be able to select a job name and then double click the report name to preview it but it gives me an error. It doesn't seem to recognize my selected job name from the list box. Error message is "Compile Error: Method or Data Member Not Found". See image.
Here is the code i'm using which is on the Double Click event on the text box in the subform.
Private Sub ReportName_DblClick(Cancel As Integer)
Dim strFilter As String
If IsNull(Me.lstJobName) Then
MsgBox "You Must Select A Job"
Me.lstJobName.SetFocus
Exit Sub
End If
strFilter = "JobName = '" & Me.lstJobName & "'"
DoCmd.OpenReport ReportName.Value, acViewPreview, , strFilter
End Sub
Trying to figure this out step by step so just need the report to preview for now. Later I will want to check off the reports that I need printed then just click a button to print.
The code is behind the subform therefore the Me. qualifier is alias for the subform and code is looking for the listbox on the subform but it is actually on the main form.
strFilter = "JobName = '" & Me.Parent.lstJobName & "'"
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 report with details of jobs/tasks, and also a form which contributes the majority of the data towards that report. Given that a report is a nice way of looking at the larger picture of the data, and a form is the best way of editing data, I would like to be able to click on a row, and have it open up the relevant record in the form view.
Does anyone know how to do this through VBA? In my mind it should be possible, though my knowledge of objects in Access is limited.
Update
I've implemented the following code for my report:
Private Sub Edit_Click()
Dim EntityName As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Entity & "'"
DoCmd.OpenForm DocName, acNormal, , EntityName
End Sub
It successfully opens the correct form, and it also grabs the correct entity name, however it isn't filtering properly. I can't see what the issue is with the code.
In your Edit_Click() procedure, you have EntityName as the WhereCondition parameter to OpenForm.
DoCmd.OpenForm DocName, acNormal, , EntityName
However, you haven't assigned anything to EntityName, so it's an empty string. I think you should use strWhere as the WhereCondition.
Private Sub Edit_Click()
Dim strWhere As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Me.Entity & "'"
DoCmd.OpenForm DocName, acNormal, , strWhere
End Sub
I assumed Entity is the name of a control, and that's where you get a value to build strWhere. If there is no control in the report by the name of Entity, then the code won't work even if there is an Entity in the original query.
You should be able to add an On Click event in the Detail section of the report, then add something like this in the VBA handler:
DoCmd.OpenForm "MyForm", acNormal, "", "ID=" & ID.Text
(where MyForm is the target form, and ID is a hidden or visible control in your report that identifies the current record).
You say report, but you should not be using a report but a continuous form or datasheet. You can then add events to any of the controls on any line, and add a double-click event, if you do not want to drive your users insane. You could also add a command button if that would be clearer to your users, or format the "link" textbox to underline. The record opened by the action shown by #dbaseman will open which even record has the focus (current record). And that will lead you to discover what you can and can't do with a continuous form :D
I have a Icon of PDF in my form that I have created in Access 2010. There are 3 tabs in that form; each tab have a separate form page and PDF icon is common for all the tabs.
Now I want that whenever a user click on that icon a PDF file of that form get created.
I have written this code
Private Sub cmdPrintReportPDF_Click()
Dim strDefaultPrinter As String
strDefaultPrinter = Application.Printer.DeviceName
**Set Application.Printer = Application.Printers("PDFCreator")**
'DoCmd.PrintOut acPrintAll
DoCmd.OpenReport "Graph_report", acViewNormal
Set Application.Printer = Application.Printers(strDefaultPrinter)
End Sub
But I'm getting the following error:
Invalid procedure call or argument on line no 4.
Set Application.Printer = Application.Printers("PDFCreator")
I have PDFCreator installed and this line, which triggers an error for you, does not trigger an error for me.
Set Application.Printer = Application.Printers("PDFCreator")
Go to the Immediate Window of the VB Editor and see if you also get an error with this line:
? Application.Printers("PDFCreator").DeviceName
If that also triggers an error, you probably don't have a printer whose DeviceName is PDFCreator. You can list the names of the printers with this procedure.
Public Sub ListPrinters()
Dim objPrinter As Printer
For Each objPrinter In Application.Printers
Debug.Print objPrinter.DeviceName
Next objPrinter
End Sub
However, with Access 2010, I think you can create a PDF without using your PDFCreator print device. At least this works for me with Access 2007, so I'm guessing it will work for you.
Private Sub cmdSaveAsPdf3_Click()
Dim strPath AS String
strPath = CurrentProject.Path & Chr(92) & "Sample3.pdf"
DoCmd.OutputTo acOutputForm, "fsubSample3", acFormatPDF, strPath
End Sub
That button click code creates a PDF (Sample3.pdf) of the form (fsubSample3) embedded in a page of my main form's tab control ... which is what I thought you wanted based on the original version of your question. Now it seems you're wanting to create a PDF of a report rather than a form. You can adapt the DoCmd.OutputTo line to use a report instead of a form.
Change this:
Set Application.Printer = Application.Printers("PDFCreator")
...to this:
Application.Printer = Application.Printers("PDFCreator")
Application.Printer is a property, not an object, so it doesn't use SET to change it.