Avatar of ClaudeWalker
ClaudeWalker
 asked on

docmd.close, acsave not working

I am closing a modified report via VBA.  I use docmd.close, "rptYearlyWPPI_Expentiures", acsaveyes and I still get prompted to save the form and corresponding subform.


here is my code:

Thanks, JOe K.
DoCmd.OpenReport "rptYearlyWPPI_Expenditures", acViewDesign, , , acHidden
                Dim sSql As String
                If IsNull([Forms]![JobsStatsInfoForm]![MonthIndicator]) Then
                    [Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
                    [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding_Null"
                Else
                    [Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_sbrpt"
                    [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding"
                    
                End If
                
                DoCmd.Close acReport, "rptYearlyWPPI_Expenditures", acSaveYes
                DoCmd.OpenReport "rptYearlyWPPI_Expenditures", acViewPreview

Open in new window

Microsoft Access

Avatar of undefined
Last Comment
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

8/22/2022 - Mon
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Wouldn't you want to use

acSaveNo

?
ClaudeWalker

ASKER
No because I'm opening the report hidden design view changing the record source, saving it, and re-opening it.  

I have 1 report that can have 2 different queries.  I'm trying to avoid creating 2 reports.

JOe K.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

You can just set the Record Source on the fly ... no need to open in hidden design view.  No need to worry about saving.  Make the record source property blank, then set on the fly.  I do it all the time.

mx
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ClaudeWalker

ASKER
won't it ask you to save when you close it?
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Nope.

Try it.  Again, I've been doing that for years ... same report, on-the-fly setting of record source.

mx
ClaudeWalker

ASKER
I tried with the open report before and after the if statement.  It either says it's "open and cannot be edited in preview mode", or it says "it's missing or not open"
DoCmd.OpenReport "rptYearlyWPPI_Expenditures", acViewPreview
                
                If IsNull([Forms]![JobsStatsInfoForm]![MonthIndicator]) Then
                    [Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
                    [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding_Null"
                Else
                    [Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_sbrpt"
                    [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding"
                    
                End If

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Move the code that sets the record source to the OnOpen event of the Report.

mx
ClaudeWalker

ASKER
it worked.  How can I edit the subreports record source

[Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"

invalid reference for the subreport line of code.  The report works just not the subreport.  How would I reference it?
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Is this

[qryWPPI_Funding subreport]

the name of the Sub Rerport *Control* ... as opposed to the name of the actual subreport?  You Control name s/b different than the Source Object, say something like rsub01 or what ever.

Then

[Reports]![rptYearlyWPPI_Expenditures].rsub01.Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"

basically, your syntax is correct.  Reports.ReportName.SubReportControlName.Report.SomeControlorProperty.

See this link for the ultimate reference in form/subform ... report/subreport systax

http://www.mvps.org/access/forms/frm0031.htm

mx

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ClaudeWalker

ASKER
Me![qryWPPI_Funding SubReport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"

I'm trying to copy on main form refering to sub1

Me!Subform1.Form.RecordSource

DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Sorry, not following ... reports or forms?

"How can I edit the subreports record source"

??

mx
ClaudeWalker

ASKER
Sorry about the lack of clarity,

Reports.  The link you posted showed examples in terms of forms.  So I figured that you could just replace the form with report.  I posted the thing saying forms, to show you were I was getting the example from on that link.


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

"So I figured that you could just replace the form with report."

You can.

ok ... at this point, what is not working ?

mx
ClaudeWalker

ASKER
Using this code I get a "you have entered an expression that has an invalid reference to the property of a report/form":

     If IsNull([Forms]![JobsStatsInfoForm]![MonthIndicator]) Then
       
        [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding_Null"
        Me![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
    Else
    .....

just to make sure I'm on the right track:
the source object is: Report.sbrptYearlyWPPI_Expenditures
the name is:  qryWPPI_Funding subreport

Thanks for your time and patience.
Private Sub Report_Open(Cancel As Integer)
    If IsNull([Forms]![JobsStatsInfoForm]![MonthIndicator]) Then
        
        [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding_Null"
        Me![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
    Else
        [Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_sbrpt"
        [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding"
        
    End If
End Sub

Open in new window

DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Re this:

     If IsNull([Forms]![JobsStatsInfoForm]![MonthIndicator]) Then
       
        [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding_Null"
        Me![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
    Else

Which like is the error occurring on ?

And qryWPPI_Funding subreport   is the Name of your Sub Report *control* ??

mx
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ClaudeWalker

ASKER
The error is happening on this line:

this line is highlighted:  Me![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Where is that code?  In the Report On Open or Form or where ?

mx
ClaudeWalker

ASKER
report onOpen()


Private Sub Report_Open(Cancel As Integer)
    If IsNull([Forms]![JobsStatsInfoForm]![MonthIndicator]) Then
        
        [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding_Null"
        Me![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_Null_sbrpt"
    Else
        [Reports]![rptYearlyWPPI_Expenditures]![qryWPPI_Funding subreport].Report.RecordSource = "qryWPPI_Funding_sbrpt"
        [Reports]![rptYearlyWPPI_Expenditures].RecordSource = "qryWPPI_Funding"
        
    End If
End Sub

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Could be a timing issue.

ok ... Put the same code - but for just the Subreport ... in the SubReport OnOpen ...  and leave the main report code in the OnOpen of main report ... if you see what I mean?

So ... the subreport will set it's on record source based on the IF/Else condition ... as will the Main report.

Modify the subreport syntax accordingly.

mx
ClaudeWalker

ASKER
it gets caught in an infinite loop where I have to ctrl break to get out of. It seems to keep formating the page.
ASKER CERTIFIED SOLUTION
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ClaudeWalker

ASKER
I'm not at work anymore sorry about the late response.  

I think that is the code I should use.  

I overlooked using me. for the code.

thank you so much, I'll test it as soon as I stop by the office either tomorrow or monday.

Have a great weekend
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

ok
ClaudeWalker

ASKER
YOU ARE A GENIUS.  Thank you very much!!!

JOe K.
ClaudeWalker

ASKER
thank you for you time
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

you are welcome.

mx