Best way to rename an auto-generated report in MS Access - ms-access

How do you assign a name to a MS Access report created via CreateReport?
Code creates a dynamic report then adds controls to the report. Last, I want to name/save the report as "Student_Scores_Report01" but it appears that CreateReport auto-generates the name for the report as Report1, Report2, etc. I've run into issues where Access creates a "Report1" but it doesn't actually exist, causing the whole project to be corrupted. This is my best guess at working around this but it seems inefficient:
Dim rpt as Report
Set rpt = CreateReport
Dim ReptNmTemp as String
ReptNmTemp = rpt.name
DoCmd.Save acReport, rpt.Name
DoCmd.Close acReport, rpt.Name
DoCmd.Rename "Student_Scores_Report01", acReport, ReptNmTemp
For Each rpt02 In CurrentProject.AllReports
If rpt02.Name = ReptNmTemp Then
DoCmd.DeleteObject acReport, ReptNmTemp
End If
Next
The For...each loop deletes the auto-generated report and avoids the corruption problem. But is there a better way? Thanks in advance.

Instead of looping you could On Error Resume Next or some other error handler.
Dim rpt as Report
Dim ReptNmTemp as String
Set rpt = CreateReport
ReptNmTemp = rpt.name
DoCmd.Save acReport, ReptNmTemp
DoCmd.Close acReport, ReptNmTemp, acSaveYes
DoCmd.Rename "Student_Scores_Report01", acReport, ReptNmTemp
On Error Resume Next
DoCmd.DeleteObject acReport, ReptNmTemp
Or use acDefault, which is actually a default parameter for both Save and Close, which means if parameter is not provided, acDefault will be used.
Dim rpt as Report
Set rpt = CreateReport
DoCmd.Save , "Student_Scores_Report01"
DoCmd.Close , , acSaveYes

Related

Access 2013: Saving multiple page PDF from single report

I've never posted before and am new to this. I'm using VBA to get a single report to save multiple PDFs. I receive an "Object missing" error message from the code I built from some of the postings here or other sites. I have tried multiple corrections, but each results in either this or a new error. The code is:
Private Sub RunErrors55COPY_Click()
On Error GoTo Err_RunErrors55_Click
Dim MyPath As String
Dim MyFilename As String
Dim uniqueID As Recordset
Dim Report_ID As Field
Dim Db As Database
MyPath = "\\test.net\files\bissvcs\Services\Errors\"
Set Db = CurrentDb()
Set uniqueID = Db.OpenRecordset("ReportIdent01")
'Loop structure may vary depending on how you obtain values
For Each uniqueID In Report_ID
MyFilename = "Bsvc Error " & Report_ID & ".pdf"
'Open report preview and auto-save it as a PDF
DoCmd.OpenReport "Report01 Error Check", acViewPreview, , "uniqueID = " & Report_ID
DoCmd.OutputTo acOutputReport, "Report01 Error Check", acFormatPDF, MyPath & MyFilename, False
'Close the previewed report
DoCmd.Close acReport, "Report01 Error Check"
Next uniqueID
Exit_RunErrors55_Click:
Exit Sub
Err_RunErrors55_Click:
MsgBox Err.Description
Resume Exit_RunErrors55_Click
End Sub
Any ideas and help would be greatly appreciated!

Access not closed when added DoCmd.Close line

I have added DoCmd.Close acQuery, "Import", acSaveNo
And my access window doesn't close even with this line of code.
Option Compare Database
Option Explicit
Public Function Import()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim intFile As Integer
Dim strFilePath As String
Dim intCount As Integer
Dim strHold
strFilePath = "C:\Transfer\FromSynapseTest\TEST.csv"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Import", dbOpenForwardOnly)
intFile = FreeFile
Open strFilePath For Output As #intFile
Do Until rst.EOF
If CDate(rst(3)) >= Date And rst(98) <> 0 Then
For intCount = 0 To rst.Fields.Count - 1
strHold = strHold & rst(intCount).Value & "|"
Next
If Right(strHold, 1) = "|" Then
strHold = Left(strHold, Len(strHold) - 1)
End If
Print #intFile, strHold
End If
rst.MoveNext
strHold = vbNullString
Loop
Close intFile
rst.Close
Set rst = Nothing
DoCmd.Close acQuery, "Import", acSaveNo
End Function
Since I'm calling the function by macro, I don't think I can do
Sub subToCloseForm
DoCmd.Close
End Sub
Also I have tried DoCmd.Close acQuery, " ", acSaveNo based on what I read http://www.blueclaw-db.com/docmd_close_example.htm : If you leave the objecttype and objectname arguments blank (the default constant, acDefault, is assumed for objecttype), Microsoft Access closes the active window
Any help would be greatly appreciated. Thank you.
You don't need code DoCmd.Close acQuery, "Import", acSaveNo at all. This command tries to close a query "Import", but you didn't open this query. You opened a recordset, based on this query and you closed the recordset correctly.
If you need to close the form with name "Import", use
DoCmd.Close acForm, "Import", acSaveNo
If you are looking to close Access completely, use:
Application.Quit
Your line of code DoCmd.Close acQuery, "Import", acSaveNo is not necessary as you are opening a recordset, not the query. rst.close and set rst = nothing is sufficient for memory management.
On a side note, I would recommend including an if statement for stepping through your recordset. If the recordset is blank, you will receive an error if left unchecked. Try inserting your for loop inside this if statement:
If not rst.eof and not rst.bof then
'for loop...
end if

Delete dynamically created report on close event

I am using Access to create a dynamic report and I would like that on close event the report to be deleted.
Right now, I am creating the report like this:
Set rpt = CreateReport
With rpt
.Visible = True
.Caption = "Calendar View"
.BorderStyle = 1
.AutoCenter = True
.ScrollBars = 0
.Printer.Orientation = acPRORLandscape
.Modal = True
.PopUp = True
.OnClose = "[Event Procedure]"
End With
'extra stuff for report
strName = rpt.Name
DoCmd.Close acReport, strName, acSaveYes
DoCmd.Rename "rptNewName", acReport, strName
DoCmd.OpenReport "rptNewName", acViewPreview, , , acWindowNormal
DoCmd.DeleteObject acReport, "rptNewName"
But, the DeleteObject will throw a error, because the report is still open..
How can I delete the report on close event?
Thank you!
Place the following, just before you line DoCmd.DeleteObject...
Do While Application.CurrentProject.AllReports(sRptName).IsLoaded
DoEvents
Loop

Access Report - show current recordsource of another form

I have a report that displays another form's recordsource. When I add some record on this form and click for report, this records is not being displayed. How can I achieve displaying what is on screen - allways ? Here is my simple code for report:
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
End Sub
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = Forms![MyForm].Form.RecordSource
End Sub
You need to close the Report before re-opening it and save the new record.
In the click event of your Open Report button insert the following:
Private Sub OpenReport_Click()
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acReport, "Test1", acSaveYes
DoCmd.OpenReport "Test1", acViewPreview
End Sub
Try requerying your Forms Recordset before opening the report:
Private Sub cmdOpenReport_Click()
Me.FilterOn = False
Me.Requery
DoCmd.OpenReport "MyReport", acViewReport
End Sub
I have an answer, I knew It's not so easy (in click event of OpenReport button):
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Answer provided by Leigh Purvis (Access MVP), a BIG thanks once more !!

MS Access export report from another database

I am basically trying to do the following (export as report as PDF):
DoCmd.OpenReport report_name, acViewPreview
DoCmd.OutputTo acOutputReport, report_name, acFormatPDF, dest, False
DoCmd.Close acReport, report_name
However I am trying to get it to reference a report object in a completely separate database. How do I modify report_name to get this to work? I've tried "[dbpath].[report_name]" to no avail. The report name has spaces in the name, if that matters.
Create a new Access application session, open the other database, and use that session as the host application for your Application.DoCmd methods.
Dim objAccess As Access.Application
Set objAccess = New Access.Application
objAccess.Visible = True ' <- useful during development
objAccess.OpenCurrentDatabase "C:\wherever\YourDatabase.accdb"
With objAccess.DoCmd
.OpenReport report_name, acViewPreview
.OutputTo acOutputReport, report_name, acFormatPDF, dest, False
.Close acReport, report_name
End With
objAccess.CloseCurrentDatabase
objAccess.Quit
Set objAccess = Nothing