I was doing more complex report work today in Access. I had need to determine if my sub-report on another sub-report was running as a subreport or not. There is the case in development where I’m just testing it, but when it is running as a sub-report it needs to check the parent report for a summary field to see whether it needs to display a particular label for each detail record.
In this case I couldn’t just globally hide or replace the sub report field from it’s parent because I needed it to compare each record. The way I decided to solve this on the fly was originally to check the Me.Parent variable.
I thought maybe I could do a “If Me.Parent Is Nothing Then” check, however this resulted in an error because Me.Parent did not even exist.
So I have a routine that checks for the existence of a property for a particular object. When I passed the object “Me” and the name of the property “Parent” it returned false in my initial tests running the report by itself. So far so good.
However, I was surprised to find out that when I ran the same function when running as a sub-report and the Parent property was valid, it still returned false.
In this case, I ended up using an On Error Goto statement to determine if the error was triggered and if so, to set a ParentExists variable to False, otherwise, to set it to true. Thiis did work, even though I much prefer not to depend on errors for live code.
So if you need to do the same, here is the code I was using in all it’s unrefactored, ugly glory:
Private Sub Detail_Format(Cancel As Integer,FormatCount As Integer) Dim HasParent As Boolean If Me.HasData Then On Error GoTo NoParent HasParent = Me.Parent.Name <> "" On Error GoTo 0 If HasParent = False Then Me.Text450.Visible = (Me.Log_ID = Me.MaxLogId) Else Me.Text450.Visible = (Me.Parent!Text443 = 0) And (Me.Log_ID = Me.MaxLogId) End If 'Me.Text451.Visible = Me.Text450.Visible Else Me.Detail.Visible = False End If GoTo Exit_Sub NoParent: HasParent = False Resume Next Exit_Sub: End Sub
Maybe I’m missing something in your explanation, but that code seems more complex than necessary. This is what I use as a generic function
Public Function IsSubreport(rpt As Access.Report) As Boolean
‘—————————————————————————————
‘ Procedure: IsSubreport
‘ DateTime: 11/09/2014
‘ Author: Colin Riddington
‘ Purpose: Checks if the specified report is opened as a subreport
‘—————————————————————————————
Dim bHasParent As Boolean
On Error GoTo Err_Handler
‘ If opened not as a subreport, accessing the Parent property raises an error:
bHasParent = Not (rpt.Parent Is Nothing)
IsSubreport=True
Exit Function
Err_Handler:
IsSubreport=False
End Function
Then in the Report_Open event:
Private Sub Report_Open(Cancel As Integer)
If Not IsSubreport(Me) Then
‘code here to do something
Else
‘other code here to do something else (if needed)
End If
End Sub
See my article: https://www.isladogs.co.uk/is-subreport/
Hmm. Yes, that is what I was originally trying to use but rpt.Parent was giving me an error as though it didn’t exist.
Perhaps the issue is that the sub report was a sub report of a sub report which in this case was returning no data as well.
So maybe this is how you have to detect nested sub reports on another sub report with no data. I’ll have to test out this theory and see if it applies.