Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

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