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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Report ObjectName dynamically with Recordset value 1

Status
Not open for further replies.

JStrand

Technical User
Nov 14, 2011
44
US
I am creating a function to loop through a list of names which will be used as criteria in the SQL. I have the framework for the loop working and want to first change the object name (BranchScorecardReport)to include the Recordset string strBranchName. That way I can loop through 160 branches giving them a unique and meaningful file name. I want the object name to be BranchScorecardReport.strBranchName.

Are there any suggestions on how to change the name of the Object Name as to include the value of the Recordset I have in the ? Thank you in advance.
--------------------------------------------------------------

Option Compare Database
Option Explicit

Function ScorecardReports()

Dim strOutputFormat As String
strOutputFormat = "PDF Format (*.pdf)"

Dim strObjectName As String
strObjectName = "BranchScorecardRprt"

'Reference to Current Database
Dim db As Database
Set db = CurrentDb()

'Identify table with names
Dim tbl As TableDef
Set tbl = db.TableDefs("Branch")

'Declare a string variable to hold the names
Dim strBranchName As String
strBranchName = ""

'Declare a counter for a loop
Dim i As Integer
i = 0

'Open a recordset against the table with names
Dim rst As Recordset
Set rst = tbl.OpenRecordset

With rst
If .RecordCount > 0 Then 'Verify records even exist first!
.MoveFirst 'Moves to the first position
Do While i <= .RecordCount And Not .EOF
strBranchName = rst!BranchName

i = i + 1
.MoveNext

Loop

Else
'Do Nothing

End If
End With

Debug.Print strBranchName

'Temporariliy Send Output from outside loop while I learn to change the Object Name to strObjectName.strBranchName

DoCmd.OutputTo acReport, strObjectName, strOutputFormat, "H:\Reports\BranchScorecardRprt", False, ""


End Function

 
BranchScorecardReport & "-" & strBranchName

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
or assuming the rs hasn't been changed or destroyed

"H:\Reports\BranchScorecardRprt" & "-" & rst!BranchName

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Thank You MazeWorX.

When I replace strObjectName with BranchScorecardReport & "-" & strBranchName I receive an error that
BranchScorecardReport is not defined.

I defined this variable earlier in the function:

Dim strObjectName As String
strObjectName = "BranchScorecardRprt"
 
OK this works for renaming report based on the recordset value where
strBranchName = rst!BranchName.

DoCmd.OutputTo acReport, strObjectName, strOutputFormat, "H:\Reports\BranchScorecardRprt" & "-" & strBranchName, False, ""

Thanks again MazeWorX!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top