General way to reference Section in report

Posted by: tskelley on 4 August 2017, 3:01 pm EST

  • Posted 4 August 2017, 3:01 pm EST

    I am trying to reference a section from within the section's format event. The current way I am handling this is by using the Section name. For example, to pass the section to my function I use the following code:

    Call MyFunction(Me.Sections("SectionName"))

    Is there a more generic way I can reference the name of the current section I am calling the code from, so my function call can be more generalized. I need to run this code for each of my sections that have fields from the database. And yes, the function does need to be handled from the format event of the specific section.

    Thank you for your help,

  • Replied 4 August 2017, 3:01 pm EST


    <span>It is
    rather difficult to give any recommendations about this, since it is completely
    depends on your particular application logic and implementation. There is one
    way to a reference to the section object from the sections collection at the
    specified index/name - use Sections.Item(index As Variant) method. But there
    are several possible approaches to pass the reference to the section object.</span>

  • Replied 4 August 2017, 3:01 pm EST

    Option Explicit

    'local variable to hold collection

    Private mCol As Collection

    Public Function Add(sGroup As DDActiveReports2.Section,Optional sKey As String) As gSubTotal

    'create a new object

    Dim objNewMember As gSubTotal

    Set objNewMember = New gSubTotal

    'set the properties passed into the method

    Set sectGroup = sGroup

    If Len(sKey) = 0 Then

    mCol.Add objNewMember


    mCol.Add objNewMember, sKey

    End If

    'return the object created

    Set Add = objNewMember

    Set objNewMember = Nothing

    End Function

    Public Property Get Item(vntIndexKey As Variant) As gSubTotal

    'used when referencing an element in the collection

    'vntIndexKey contains either the Index or Key to the collection,

    'this is why it is declared as a Variant

    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)

    Set Item = mCol(vntIndexKey)

    End Property

    Public Property Get Count() As Long

    'used when retrieving the number of elements in the

    'collection. Syntax: Debug.Print x.Count

    Count = mCol.Count

    End Property

    Public Sub Remove(vntIndexKey As Variant)

    'used when removing an element from the collection

    'vntIndexKey contains either the Index or Key, which is why

    'it is declared as a Variant

    'Syntax: x.Remove(xyz)

    mCol.Remove vntIndexKey

    End Sub

    Public Property Get NewEnum() As IUnknown

    'this property allows you to enumerate

    'this collection with the For...Each syntax

    Set NewEnum = mCol.[_NewEnum]

    End Property

    Private Sub Class_Initialize()

    'creates the collection when this class is created

    Set mCol = New Collection

    End Sub

    Private Sub Class_Terminate()

    'destroys collection when this class is terminated

    Set mCol = Nothing

    End Sub


    Option Explicit

    Public WithEvents sectGroup As DDActiveReports2.Section

    Private Sub sectGroup_Format()

    Call MyFunction(sectGroup)

    End Sub
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels