Results 1 to 12 of 12

Thread: [RESOLVED] getting variables to function over multiple forms

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Resolved [RESOLVED] getting variables to function over multiple forms

    Hi there, im a student doing a project in vb6 and need help.

    Basically, im trying to get an array of 3 numbers to be randomly generated in one form and then displayed in another form. I tried using public but it says i cant do that (something about it being an array).

    So then i tried dimensioning 3 variables that would equal positions 1, 2 and 3 of the array, because you can dimension them as public. But when i tried that, it said that the variables were not defined in the next form.

    any help is much appreciated!


  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: getting variables to function over multiple forms

    Where are you declaring them?

    If you declare them in a module, then you can use them as is anywhere.

    If you declare them on a form, then to be used else where you need to preface them with the form name.

    Example:
    Code:
    'in a module
    Public Hack As String
    
    'on a form
    Hack = "Test"
    
    'on form1
    Public Hack As String
    
    'on another form
    Form1.Hack = "Test"

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: getting variables to function over multiple forms

    Cal

    I'm not an expert on this, but basically arrays dimmed on
    a Form (even using Public) still have a "scope" limited to
    only that Form.

    One way around this is to add a Module to your project
    and dim the array as Public in the declaractions section
    of the Module. For the time being, this is probably all that
    you will have in your Module, but that's ok.

    Others may have more ideas, but I hope this one gets
    you started.

    Spoo

  4. #4
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: getting variables to function over multiple forms

    Quote Originally Posted by Hack View Post
    If you declare them on a form, then to be used else where you need to preface them with the form name.
    Hack ... will that work with arrays?

    Spoo

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: getting variables to function over multiple forms

    thanks very much guys. Great help. Il try both ways and see wat works and post back.

    cal

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: getting variables to function over multiple forms

    Cal

    My humble guess is that Hack's idea will not work with arrays
    .. that is, the one where one preceeds the array by the form name.

    Of course, Hack never specifically suggested that it would
    work with arrays... just to clarify, he was only talking about variables.

    1. You can't dim an array as Public on a form
    2. You can dim an array on a form -- as in Dim aaArray(5, 5) -- on
    a form, but it can't be "seen" as an array on another form, even
    if preceeded by the original Form's name

    Spoo
    Last edited by Spoo; Feb 16th, 2010 at 03:22 PM. Reason: add sentence "Of course, Hack never ... "

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: getting variables to function over multiple forms

    Spoo
    Could u tell me in code how to dim the array as public in a module? I tried doing this:

    Code:
    Private Sub user_numbers(ByRef lott() As String)
    
    Public lott_num(3) As Integer
    End Sub


    It doesnt work when i try to call it in the next form it says "sub or function not defined"...should i call it in the general declerations part of the next form or have i done it wrong? I know this is a nooby thing to not know but im still learning this in school so bear with me

  8. #8
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: getting variables to function over multiple forms

    Cal

    No Sub is needed.

    In the Declaractions section of the module (ie, at the very top)
    all you need is

    Code:
    Public lott_num(3) as Integer
    Spoo

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: getting variables to function over multiple forms

    Spoo

    when i did this it says "Compile error: constants, fixed-length strings, arrays, user defined types and Declare statements are not allowed as Public members of object modules."

    This was the method i tried at the start and got this message.

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: getting variables to function over multiple forms

    Quote Originally Posted by calarexar View Post
    ... when i did this it says "Compile error: constants, fixed-length strings, arrays, user defined types and Declare statements are not allowed as Public members of object modules."

    This was the method i tried at the start and got this message.
    They need to be declared in a bas module, not the form.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: getting variables to function over multiple forms

    LaVolpe,

    yeah, just worked that out and have done it, like literaly as soon as i got this post! Thanks guys, massive help!
    Last edited by calarexar; Feb 17th, 2010 at 02:35 PM.

  12. #12
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: [RESOLVED] getting variables to function over multiple forms

    Don't know about the OP calling themselves a newbie.

    I never knew this about arrays in VB. Never actually tried to do it of course but surely you can pass them as an address like in C++. I am sure I have read here that you can do that with variables and an array is just a group of variables in memory.

    A very interesting and informative thread. Many thanks to the OP for starting it and everybody who has contributed.
    Last edited by AsmIscool; Feb 20th, 2010 at 11:23 PM.
    Slower than a crippled Vista
    More buggy than a fresh XP install
    Look! Down the road, some 50 miles behind the drunken snail.
    It's Ubuntu!

Posting Permissions

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



Click Here to Expand Forum to Full Width