Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Report Preview Without Saving 1

Status
Not open for further replies.

Eddi Rae

Programmer
Jan 31, 2021
10
US
I create a report where I change the report source every time the report is created. I open the report in the Print Preview mode. When I go to close the report, it asks me to save the report objects. I don't want to save them, I just want to close the report.

I am not seeing how to do this.
Any help is greatly appreciated.
Eddi Rae
 
Before I provide a suggestion, are you using code to open the report or manually doing this?
 
I am using code. In fact, I changed my process, but I am still getting the save to pop up.
Here is my new code.
Code:
    ' OPEN REPORT
    DoCmd.OpenReport "rptPrtArticleInspectionSheets", acViewDesign
    
    ' ADJUST SOURCE
    Reports!.Report.RecordSource = strSQL
    Reports!!.Report.RecordSource = strSubSQL
    
    'CLOSE REPORT
    DoCmd.Close acReport, "rptPrtArticleInspectionSheets", acSaveYes
    
    'OUTPUT TO FOLDER
    DoCmd.OutputTo acOutputReport, "rptPrtArticleInspectionSheets", acFormatPDF, strPDFname
    
    MsgBox strPDFname & " has been created"

The output to the folder works great, but it still asks if you want to save the report sections prior to the output of the file.
 
 https://files.engineering.com/getfile.aspx?folder=c1aa0c67-ceff-447b-a1fd-a2124c4b43fe&file=Forum_Picture_1_20210209.docx
I would use a little DAO code to change the SQL of a saved query that is the record source of the report. This would remove the requirement to open the report in design view.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
That sounds like a good idea! I will try it and let you know how it works!!
 
Sorry for delay. If you have a question/problem setting up the code, let me know.

Here is a suggestion for the report and recordsource. Rather than opening the report and adjusting the sql statement and saving the report, use a "standard query" and edit the query.


Since your example is using an sql statement the code will use that as the basis.
This is a one time setup
1. Add or create a new standard code module and paste this code

Code:
Function EditQryDef(ByVal stQueryName As String) As DAO.QueryDef
'[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1758828[/URL]
'20151119
    On Error GoTo NewQueryDef
    
    Set EditQryDef = CurrentDb.QueryDefs(stQueryName)
    
QryDef_Exit:
    Exit Function
    
NewQueryDef:
    Set EditQryDef = CurrentDb.CreateQueryDef(stQueryName)
    Resume QryDef_Exit
End Function


2. Create the two queries that you want to for your report and sub report and save it with the name qryForReport and qryForReportSub (or some other generic yet meaningful name). These query names will not change only the contents of the sql will get changed

3. Open the report manually in design view and change the record source to qryForReport and qryForReportSub, then save it.



In your existing code try this. I left your code in and commented out so that you would know where to put it. The Dims go at the top of the code regardless. The Set to Nothing goes at the end of the code.

Code:
Dim qd As DAO.QueryDef
Dim strsql as String
Dim strSubSQL as String

' OPEN REPORT
    'DoCmd.OpenReport "rptPrtArticleInspectionSheets", acViewDesign
    
'     ADJUST SOURCE
    'Reports!.Report.RecordSource = strSQL
    'Reports!!.Report.RecordSource = strSubSQL
    Set qd = EditQryDef("qryForReport")
    qd.sql = strSQL
    
    Set qd = EditQryDef("qryForReportSub")
    qd.sql = strSubSQL
'   CLOSE REPORT
    'DoCmd.Close acReport, "rptPrtArticleInspectionSheets", acSaveYes
    
    'OUTPUT TO FOLDER
    DoCmd.OutputTo acOutputReport, "rptPrtArticleInspectionSheets", acFormatPDF, strPDFname
    
    MsgBox strPDFname & " has been created" 

Set qd = Nothing
 
Thanks dhookom. I took your concept and did it a little different.
I created two queries; one for the main report and another for the sub report.
I changed the report to use those two queries so that they wouldn't change.
When I process the report, I update those two queries and then output the report to a pdf file.

Here is my code now.

Code:
    ' ADJUST SOURCE
    CurrentDb.QueryDefs("qryRptArticleInspection_Main").SQL = strSQL
    CurrentDb.QueryDefs("qryRptArticleInspection_Sub").SQL = strSubSQL
        
    'OUTPUT TO FOLDER
    DoCmd.OutputTo acOutputReport, "rptPrtArticleInspectionSheets", acFormatPDF, strPDFname
    
    MsgBox strPDFname & " has been created"

It works perfect!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top