[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!
:):):)
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"
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
Re: getting variables to function over multiple forms
Quote:
Originally Posted by
Hack
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
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
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
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 :)
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
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.
Re: getting variables to function over multiple forms
Quote:
Originally Posted by
calarexar
... 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.
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!:)
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.