I hope I have followed the posting guidelines; apologies from a neophyte if not, and please correct me. I'm self-taught in VB and have reached my limit, so I'm looking for help. I am dealing with multiple permutations: source report, report name, destination, etc. I've created a form where the user selects options; I have functional code (thanks in part to postings here) but am trying to pull out repetitive portions into a public sub so if there are changes to the main variables I only have to make them once. Here's the relevant functional code:
My problem is that I have to repeat this for each of 6 reports (internal/external and by course/by instructor/ by team). The two other variables fall where this snippet has \Internal (following %O) and ByInstructor_ (between %R and %T). I tried making this code (minus the line with those variables) a public sub and calling it from a private sub but I can't get it to work. In "Public Sub DynamicReportNa me()" I changed the line with variables to read
strReportName = "%O%I%R%N%T%Y_% D.pdf"
but left the rest of the code defining the variables the same. I confirmed that this generates a report name with the appropriate variables for O, R, T, Y, and D, and leaves %I and %N waiting for replacement. Then in the individual sub I tried this:
When that didn't work (I got the standard Windows dialog box asking for the file destination/name) I tried outputting "strReportN ame" to a dialog box to see what I have (I haven't figured out how to use the Immediate window, I only know of its existence), and it's blank. I thought perhaps the problem was that I needed to use one name in the public sub (strOutput) and another in the private sub (strReportName) , setting strReportName = strOutput, but that didn't work either.
I'm using Access 2007.
Thanks for any help you can provide! Worst case scenario is I modify 6 times if the drive designations or teams change, but I'm hoping to learn something.
Code:
' creating dynamic report output destination and name
' %O = output drive, %R = responsible team, %T = term, %Y = academic year, %D = date
Dim strReportName As String
strReportName = "%O\Internal_%RByInstructor_%T%Y_%D.pdf"
strReportName = Replace(strReportName, "%O", IIf(Forms!frmMainMenu!optgrpDestination = 1, "H:", "O:"))
strReportName = Replace(strReportName, "%R", Switch(Forms!frmMainMenu!optgrpResponsibleTeam = 1, "All", Forms!frmMainMenu!optgrpResponsibleTeam = 2, "Education", Forms!frmMainMenu!optgrpResponsibleTeam = 3, "Practice", Forms!frmMainMenu!optgrpResponsibleTeam = 4, "Research"))
strReportName = Replace(strReportName, "%Y", Me.cboYear)
strReportName = Replace(strReportName, "%T", Me.cboTerm)
strReportName = Replace(strReportName, "%D", Format(Date, "yy-mm-dd"))
strReportName = "%O%I%R%N%T%Y_% D.pdf"
but left the rest of the code defining the variables the same. I confirmed that this generates a report name with the appropriate variables for O, R, T, Y, and D, and leaves %I and %N waiting for replacement. Then in the individual sub I tried this:
Code:
Dim strReportName As String
Call DynamicReportName
strReportName = Replace(strReportName, "%I", "\Internal_")
strReportName = Replace(strReportName, "%N", "ByInstructor_")
I'm using Access 2007.
Thanks for any help you can provide! Worst case scenario is I modify 6 times if the drive designations or teams change, but I'm hoping to learn something.
Comment