[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DoCmd.OutputTo - Formatting Title of Report

Posted on 2014-08-03
6
Medium Priority
?
400 Views
Last Modified: 2014-08-04
I am creating a DoCmd.OutputTo for an Access report in a PDF format.  I am having trouble with the syntax for the title.

Here is the code I have:

Private Sub Print_Transfer_Report_Click()

Dim vFrom As String
Dim vTo As String

vFrom = Format([Forms]![FrmTransfRpt]![txtFrom], "mm/dd/yy")
vTo = Format([Forms]![FrmTransfRpt]![TxtTo], "mm/dd/yy")
DoCmd.OutputTo acReport, "rpt_TransferReport", "PDF Format(*.pdf)", _
"\\pmcfs\groups\HR\HR Extractions\TransferRpts\Employee Transfers From_" _
& vFrom & "_To_" & " " & ".PDF", False, 0

DoCmd.Close acReport, "rpt_TransferReport"
End Sub


I want to add the input the From and To date from the form "FrmTransfrRpt".  The fields are text boxes with a short date format, txtFrom and txtTo.

When I type in the code, the Access program overwrites the the txtTo field name with TxtTo.

I have tried to save the values to a temporary variable but that does not work either.

If I change the areas for the dates to a " ", the report runs.

Can someone help with the proper syntax?

Thanks

Glen
0
Comment
Question by:GPSPOW
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40237809
The outputformat argument is acformatPDF.  Also, I recommend saving the proposed save name to a String variable and inspecting it in the Immediate Window with a Debug.Print statement.  If you want a date in the save name, make sure you use a format that doesn't use slashes, as they aren't allowed in file names.  Format(dteTest, "m-d-yyyy") would work.

Public Sub ExportReportToPDF()
'Created by Helen Feddema 12-Jul-2013
'Last modified by Helen Feddema 12-Jul-2013

On Error GoTo ErrorHandler

   Dim appOutlook As New Outlook.Application
   Dim strReport As String
   Dim strSavePath As String
   Dim strReportFile As String
   Dim itm As Outlook.MailItem
   Dim strEmail As String
   
   strReport = "rptCurrentData"
   strSavePath = "G:\Documents\PDFs\"
   strEmail = "hfeddema@hvc.rr.com"
   strReportFile = strSavePath & "Current Data.pdf"
   
   'Create PDF file
   DoCmd.OutputTo objecttype:=acOutputReport, _
      objectname:=strReport, _
      outputformat:=acFormatPDF, _
      outputfile:=strReportFile, _
      autostart:=True
   
   'Create email
   strEmail = "Email address"
   Set itm = appOutlook.CreateItem(olMailItem)
   itm.Subject = "Message Subject"
   itm.Body = "Message body"
   itm.To = strEmail
   itm.Attachments.Add Source:=strReportFile, _
      Type:=olByValue
   itm.Display
   
ErrorHandlerExit:
   Set appOutlook = Nothing
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: "
   Resume ErrorHandlerExit

End Sub

Open in new window

0
 

Author Comment

by:GPSPOW
ID: 40237891
Helen,

How can I add the values from the form input fields txtFrom and txtTo to the "strReport" field value?

'Create PDF file
   DoCmd.OutputTo objecttype:=acOutputReport, _
      objectname:=strReport, _
      outputformat:=acFormatPDF, _
      outputfile:=strReportFile, _
      autostart:=True

Thanks

Glen
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40238207
Using Your original code, try something like this:

Private Sub Print_Transfer_Report_Click()

Dim vFrom As String
Dim vTo As String

vFrom = Format([Forms]![FrmTransfRpt]![txtFrom], "mm/dd/yy")
vTo = Format([Forms]![FrmTransfRpt]![TxtTo], "mm/dd/yy")
Private Sub Print_Transfer_Report_Click()

Dim vFrom As String
Dim vTo As String

vFrom = Format([Forms]![FrmTransfRpt]![txtFrom], "mm-dd-yy")
vTo = Format([Forms]![FrmTransfRpt]![TxtTo], "mm-dd-yy")
DoCmd.OutputTo acReport, "rpt_TransferReport", acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\TransferRpts\Employee Transfers From_" & vFrom & "_To_" & vTo & ".PDF", False, 0

DoCmd.Close acReport, "rpt_TransferReport"

End Sub
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 2000 total points
ID: 40238208
Private Sub Print_Transfer_Report_Click()

Dim vFrom As String
Dim vTo As String

vFrom = Format([Forms]![FrmTransfRpt]![txtFrom], "mm-dd-yy")
vTo = Format([Forms]![FrmTransfRpt]![TxtTo], "mm-dd-yy")
DoCmd.OutputTo acReport, "rpt_TransferReport", acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\TransferRpts\Employee Transfers From_" & vFrom & "_To_" & vTo & ".PDF", False, 0

DoCmd.Close acReport, "rpt_TransferReport"

Open in new window

0
 

Author Closing Comment

by:GPSPOW
ID: 40238885
Thanks to all who submitted a solution.  This was what I was looking for.  The format of the date was acceptable for a file name.  the solution before had the "/" in it so it would not work.

Glen
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40239228
Thanks...

Also note that if you do his twice in the same day, the original will be overwritten (lost, unrecoverable)

So it is advised to include the current date and time to all exported reports, ...something like this:
DoCmd.OutputTo acReport, "rpt_TransferReport", acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\TransferRpts\Employee Transfers From_" & vFrom & "_To_" & vTo & DoCmd.OutputTo acOutputReport, "rptHousesWithOptimum", acFormatPDF, "c:\Junk" & "_CreatedOn_" & Format(Now, "YYYY-MM-DD hh_nn_ss") & ".pdf"
.PDF", False, 0

This way you can keep a history of all the reports.

As a benefit, the format: "YYYY-MM-DD hh_nn_ss"
...will allow the files to be sorted correctly by date.

JeffCoachman
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question