In an Access 2003 database, I have an employee status/feedback report that is generated through VBA. I create the report in VBA because the desired formatting of report is such that I found it to be easier for me to do it this way (combining multiple recordsets, displaying more than one record from a recordset aligned with others, customizing some formatting elements based on department, etc.). There may be a better way to create these reports, but this is functioning just fine, so I’d rather avoid recreating if possible.
My problem is this: As this is an employee feedback form, there are details that supervisors want to be able to see, but not show the employee. When the report is generated, the supervisor has a chance to review the data/make changes from the report itself (via userforms, event handling on the report). Part of the review is to look at one set of fields (added to the report as labels), but these fields should be hidden when the report is ultimately printed.
In my head, I thought I had seen before a way to hide specific controls in this way and did some googling to find this MSDN resource on DisplayWhen. However, it appears that DisplayWhen is usable only on forms. Is there a way to set this value (or similar/related option) using reports? Doing a search on SO, I found only one single question in a query for DisplayWhen, which was not helpful to me.
Using .Visible = False on the creation of the report of the report won’t work because the supervisors will need to see those fields. It does not appear that I can set this property upon printing, as I see no event handling option for this. Can I create a custom event handler that will capture the print command?
Here’s the control creation code:
Private Sub AddOneOnOneField(Rpt As Report, Left As Integer, Top As Integer, Width As Integer, Height As Integer, _
Optional Cap As String = vbNullString, Optional Align As Integer = cnFontCenter, Optional Size As Integer = 10, _
Optional Bold As Integer = cnBoldFont, Optional DisplayWhen As Byte = 0)
With CreateReportControl(Rpt.Name, acLabel, acDetail, vbNullString, vbNullString, _
Left, Top, Width, Height)
.Properties("FontSize") = Size
.Properties("FontWeight") = Bold
.Properties("BorderStyle") = 1
.Properties("TextAlign") = Align
.Properties("Caption") = Cap
.Properties("DisplayWhen") = DisplayWhen ' This does not work!
End With
End Sub
To reiterate my questions:
- How can I set certain report fields to only display on print preview, but not on print?
- Can I create a custom event handler that will capture the print command from a report?
- Is there a way to set the
DisplayWhenproperty (or similar/related option) on a report field?
This was not ideal, but it works. When creating the report through VBA, I already had code to create code within the report module. I added to that function to capture the print/force hiding of the fields, print, then un-hide.
For readability, here’s the actual code that is in the module after this runs:
When adding new controls to the report (see original code in question body), I added the ability to optionally assign a name to the control:
The control creation code was called with a
NewNameargument of"HIDE" & ControlTop & ControlLeftto allow for naming all the needed controls without duplicating names. Before hiding, the report is opened in design mode to prevent screen flickering/slowdown. The printing code then cycles through all the controls on the report looking for names starting with “HIDE” and hides them. Once all controls are hidden, the report is reopened as normal to print, then the process repeats to unhide these controls and display the original report to the user.I still was not able to capture the
printevent properly (and this is where the solution becomes not my favorite option), so instead I prompted the user at the initial creation of the report to print, calling the newly createdCapturePrintSub (while hiding those fields). If they opt not to, then they can print as normal without hiding the fields, or regenerate the report to hide them/be prompted again.