PDA

View Full Version : Referencing Variables in One Form From Another



Soul777Toast
02-27-2008, 03:00 PM
Hi All, this is my first post here!

My question is, is there any way to access the value of a variable that has been declared in one form from another form without having to use a global variable?

The specifics: I have a form (formA) with a button that launches another form (formB) with textboxes for values and an add button, and allows the user to enter in dimensions for a chart that will be created later. I want to allow the user to do this as many times as they want (to create as many charts as desired), and I want to store these dimensions in a variable size multidimensional array (arrayA) in formA (so I can pass them to a function that will create the forms), so the process I'd like to follow is to store the values in the array of dimensions in a temporary array (arrayB) in formB, redimension formA to add a new record, repopulate arrayA with its original values, and then add the new values into the new space in arrayA. To do this, I will need to be able to access arrayA which is declared in formA from formB.

I know that you can access the values of controls in one form from another (for example, formname.controlname.value), which is what makes me wonder if it is possible to do this with variables in some way (I've tried formname.variablename, but it doesn't work).

I also know that I could just use a globabl variable, but I'm a bit of a code perfectionist and I don't like using global variables if I can avoid it ;P

Any help here would be appreciated, thanks in advance!

Bob Phillips
02-28-2008, 01:22 AM
Declare it as a pub;ic propert in Forma



Public FormAArray As Variant


abnd access that from FormB like so



FormBArray = FormA!FormAArray


and so on

Soul777Toast
02-28-2008, 05:37 AM
Is there something I need to do to be able to declare a public variable in a form? Right now I get an "Invalid Attribute in Sub or Function" error at compile. Also, wouldn't declaring the array as public be pretty similar to just using a global variable as far as scope goes?

Bob Phillips
02-28-2008, 05:47 AM
Public Properties are not part of a sub, they are a separate declaration of their own. If you are using Let/Get property declarations, they can be anywhere in the code, but if you jsut declare a single Raed/Write property it should be declared before any procedures.

Soul777Toast
02-28-2008, 05:52 AM
That still doesn't answer my question as to scope. Wouldn't using a public variable be pretty much the same thing as using a global one (which I'm trying to avoid)?

mikerickson
02-28-2008, 06:00 AM
Yes, Global variable is another term for Public variable.

Both should be avoided when there is a better alternative.

Soul777Toast
02-28-2008, 06:08 AM
So, what's the better alternative?

mikerickson
02-28-2008, 06:25 AM
In this case, it looks like a Public variable is the best solution.

Principles like "avoid loops" and "avoid Public variables" are for comparing alternate methods. Until there is an alternative, "bad practice" is the best practice.

I would declare your Public array in a normal module, probably the one that holds the ShowUserform1 routine, before any of that proceedures in the module.

Bob Phillips
02-28-2008, 07:17 AM
No it is a public property not a public variable, it is only within the scope of the class, the userform in this case.

But by all means carry on bad practice if you think it is best.

Soul777Toast
02-28-2008, 07:32 AM
alright, thanks guys. After some research, I think the method I will use will be to declare the array as Freind in formA, allowing me access to the value without quite as much opportunity for outside access. The other thing I'm toying with that I just learned about is possibly using a collection instead of an array, allowing me to skip the redimensioning process and just use the collection.add property. Might be a little easier.

Soul777Toast
02-28-2008, 08:15 AM
Never mind, I just realized that friend only works for subs and functions, not for variables. Public it is.