Refering to an array in a parent form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • John Crozier
    New Member
    • Jul 2010
    • 4

    Refering to an array in a parent form?

    Hello! I would like to retreive data from an array in a parent form. I think it's best explained by showing an example...

    Parent Form
    Code:
    Option Compare Database
    Dim ChildNum(1 To 255) As Integer
    ...
    'Inside a sub I have a loop with this inside it...
    ChildNum(ii) = Nz(rstPODetails!RecordId)
    Child Form
    Code:
    MsgBox Me.Parent.ChildNum(Me.CurrentRecord)
    Can anyone see where I have placed a wrong foot? How would you go about accessing this array from a child?

    Any help would be much appreciated

    John
  • tasawer
    New Member
    • Aug 2009
    • 106

    #2
    Hi,

    My understanding is that your Array would only be accessible from within the Private Sub that it was defined in.

    The option that comes to my mind is to work with the Array in Public Sub (i've never done this before) or save the values of an Array to a table and then reference the table from subform.

    I hope this suggestion helps

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      To access data within an Array initialized and populated in a Main Form, from the context of a Sub-Form, you can Declare the Array as 'Public' within a Standard Code Module. In this manner its contents can be accessed from anywhere within your Application as illustrated below:
      1. Array Declaration in a Standard Code Module:
        Code:
        Public ChildNum(1 To 255) As Integer
      2. Accessing the First and Last elements of the Array, once initialized and populated:
        Code:
        Debug.Print "First Element of ChildNum(): " & ChildNum(1)
        Debug.Print "Last Element of ChildNum(): " & ChildNum(255)

      Comment

      • John Crozier
        New Member
        • Jul 2010
        • 4

        #4
        Thanks very much for the replies. Something inside me dislikes using modules for single forms so I found another way via ADO rst's on the parent form. It's a lot more complicated than using an array would have been, but it works, and has allowed a little more flexibility.

        Thanks very much for the info both of you :)

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Something inside me dislikes using modules for single forms
          Hello John. You are not technically using a Single Form, but two independent Forms involved in a Parent <==> Child Relationship as I see it. This is why the Declaration is needed in a Standard Code Module, and must be Public in Scope.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #6
            There is no absolute need to define this array in a separate, standard module. This can also be defined as Public within the parent form (not within any procedure of course - in fact just change your original line #2 to say Public in place of Dim). It would then be referred to (from the subform) as Me.Parent.Child Num() (exactly as you had it in fact).

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Hello NeoPa. Isn't this approach sort of frowned upon? By Declaring ChildNum() As Public in the Parent Form, you have now made it a Method of that Form. This isn't exactly the traditional manner in which Methods are created, mainly within a Class Module. This is analogous to Declaring a Variable in a Form's Class Module as Public, now making it a Property of the Form, bypassing Property Let...Get Procedures.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32645

                #8
                More than analogous ADezii. It's actually declaring the item as a property (rather than as a method).

                In a normal class module that is indeed frowned upon, but within a form's class module this is much less likely to be an issue. Individual developers can decide for themselves whether the side-effects - the lack of control - is even relevant to their own scenario (when dealing with anything which is not planned for general consumption that is - such as a form class).

                Remember, the main reason for said advice is to ensure the class module is portable to as many varied environments as possible, as well as structurally sound, so as to require less in the way of support. This is simply not an issue in most cases when dealing with a form.

                Compared with adding it to a generic module, as opposed to defining a fully featured and properly structured class with Lets and Gets etc, I see no downsides. It is, after all, an item relevant to the form object. Defining it in a general module, though that works well and does the job, I would consider to be less of a pure approach rather than more of one.

                Comment

                Working...