How would i refer to a closed report?

C

Chris K

I want to copy the filter on my form to the filter of a close report:

Dim stDocName As String

stDocName = "Myreport"
DoCmd.OpenReport stDocName, acNormal

With Reports("Myreport")
.Filter = Filter
.FilterOn = True
End With


I get an error because the report is close and my syntax only access open
report

How would i refer to a closed report?
 
D

Douglas J. Steele

Opening a report using acNormal prints it.

You'll need to use acViewPreview, change the filter and then print it.
 
C

Chris K

Thats good idea but it leaves the reports open (behind the form) and this is
for 3rd part users - is there anyway to close the report after the print
instruction?
 
D

Douglas J. Steele

Looking at this again, why not just use

Dim stDocName As String

stDocName = "Myreport"
DoCmd.OpenReport stDocName, acNormal, , Filter


--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele
Co-author: "Access 2010 Solutions", published by Wiley
(no e-mails, please!)
 
C

Chris K

I could but I need to do other things with the report including setting the
recordsource (same as the form)

Private Sub PrintAReport_Click()
On Error GoTo Err_PrintAReport_Click

Dim stDocName As String

With Reports("Main Report")
.RecordSource = RecordSource
.OrderBy = OrderBy
.OrderByOn = True
.Caption = recordsource
End With

stDocName = "Main Report"
DoCmd.OpenReport stDocName, acViewPreview


DoCmd.OpenReport stDocName, acViewNormal
DoCmd.Close acReport, RecordSource

Exit_PrintAReport_Click:
Exit Sub

Err_PrintAReport_Click:
MsgBox Err.Description
Resume Exit_PrintAReport_Click

End Sub


It wont let me set recordsource when the report is open and i cant address
it when it's closed so i seem to be back where i started, trying to address
a closed report

cant use something like

With Database.Objects.Reports("My Report")
 
J

John W. Vinson

It wont let me set recordsource when the report is open and i cant address
it when it's closed so i seem to be back where i started, trying to address
a closed report

One technique I've used (I forget who posted it to these groups but I'm
grateful!) is to base the Report on a query referencing a Form (frmCrit let's
say), and actually open frmCrit in the Report's own Open event:

Private Sub Report_Open(Cancel as Integer)
DoCmd.OpenForm "frmCrit", WindowMode:=acDialog
<other open code>
End Sub

When the form opens in dialog mode, all code action stops until the form is
closed or made invisible; so you can put a command button on frmCrit whose
only action is

Me.Visible = False

This will resume opening the report, with the criteria specified on frmCrit.
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/Forums/en-US/accessdev/
http://social.answers.microsoft.com/Forums/en-US/addbuz/
and see also http://www.utteraccess.com
 
C

Chris K

I'd actually moved the code from the form to the open report - initially i
had 12 reports so I avoided it but by using code I've made one report do all
those jobs which meant it's more practical to put the code in the report and
refer to the form (which will always be open):

Private Sub Report_Open(Cancel As Integer)

With Forms("JHPCLIENTS")
RecordSource = .RecordSource
OrderBy = .OrderBy
OrderByOn = True
Label22.Caption = .RecordSource
End With


End Sub
 
D

David W. Fenton

I could but I need to do other things with the report including
setting the recordsource (same as the form)

Setting the recordsource of a report is usually done in the report's
OnOpen event. This is because until recent versions of Access, you
couldn't open reports hidden nor modally. While you can now do those
things, I'd still say the proper place to set the recordsource is
the OnOpen event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Exporting a Report to Excel 2
Cancel a Report from a form 7
Syntax for number of copies to print? 1
Applying a filter on a report 2
Print Button Code 2
Command button plus message 2
Command Button 1
Report VB Code 1

Top