473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Dim ChildNum(1 To 255) As Integer
  3. ...
  4. 'Inside a sub I have a loop with this inside it...
  5. ChildNum(ii) = Nz(rstPODetails!RecordId)
Child Form
Expand|Select|Wrap|Line Numbers
  1. 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
Sep 3 '10 #1

✓ answered by ADezii

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:
    Expand|Select|Wrap|Line Numbers
    1. Public ChildNum(1 To 255) As Integer
  2. Accessing the First and Last elements of the Array, once initialized and populated:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print "First Element of ChildNum(): " & ChildNum(1)
    2. Debug.Print "Last Element of ChildNum(): " & ChildNum(255)

7 2350
tasawer
106 100+
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
Sep 3 '10 #2
ADezii
8,834 Expert 8TB
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:
    Expand|Select|Wrap|Line Numbers
    1. Public ChildNum(1 To 255) As Integer
  2. Accessing the First and Last elements of the Array, once initialized and populated:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print "First Element of ChildNum(): " & ChildNum(1)
    2. Debug.Print "Last Element of ChildNum(): " & ChildNum(255)
Sep 3 '10 #3
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 :)
Sep 4 '10 #4
ADezii
8,834 Expert 8TB
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.
Sep 4 '10 #5
NeoPa
32,556 Expert Mod 16PB
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.ChildNum() (exactly as you had it in fact).
Sep 7 '10 #6
ADezii
8,834 Expert 8TB
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.
Sep 7 '10 #7
NeoPa
32,556 Expert Mod 16PB
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.
Sep 7 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Kathy D | last post by:
I have created an mdi parent form with four children. I want to disable closing the four children but allow closing the parent form. I created a FormClosing event on each form. That event tests...
3
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child...
4
by: Thorsten Ottosen | last post by:
Dear all, Is it possible to close or hide a parent form? In particular, is it possible to do from the child form itself? Thanks Thorsten
1
by: Sylvain | last post by:
Hi, I am developping a Visual C++ application. In my application, I created a Mdi parent form with a main menu. I also created a "menuItem" click to display a Mdi child form. I want in this case...
2
by: Paul | last post by:
Hi this is related to a previous post, hopefully just a bit clearer description o the problem. I have a parent form that opens a new form (child form) while still leaving the parent form open....
2
by: Mike L | last post by:
The child form can be dragged out of the Parent form. I set the child form to IsMdiContainer = False and the Parent Form IsMdiContainer = True. I also coded in the Parent Form on load, Dim f As...
4
by: eBob.com | last post by:
I have a "parent" form (if that's the right terminology), Form1. I declare two public values in the parent form : Public Class Form1 Inherits System.Windows.Forms.Form Public CurDir As String =...
0
by: Ben | last post by:
Please excuse if this ends up being a double post at some point. I tried posting using another website which crashed, so I am not sure if it will ever get posted. As such I decided to try again...
2
by: Matt | last post by:
Ok here is my problem: I have a MDI parent form called "Main" that I declare in a public module when I start up my program. This form holds the drop down menu that allows my users to access all...
2
by: dynamictiger | last post by:
I am building a wizard and have an issue with one sub form. Whilst all others are about 5 fields tall this one is 15 fields. My choices are either to resize the parent form ridiculously large for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.