Consulting

Results 1 to 11 of 11

Thread: Referencing Variables in One Form From Another

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location

    Referencing Variables in One Form From Another

    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!
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Declare it as a pub;ic propert in Forma

    [vba]

    Public FormAArray As Variant
    [/vba]

    abnd access that from FormB like so

    [vba]

    FormBArray = FormA!FormAArray
    [/vba]

    and so on
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location
    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?
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location
    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)?
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Yes, Global variable is another term for Public variable.

    Both should be avoided when there is a better alternative.

  7. #7
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location
    So, what's the better alternative?
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location
    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.
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

  11. #11
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location
    Never mind, I just realized that friend only works for subs and functions, not for variables. Public it is.
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •