Re: pass variable to form?
you want it to be public?
create a module (BAS) and put your "global" public variables in there like this
in you module put this
Code:
Public MyArray(0 To 100) As String
then you can use in any other forms in the same project... not only 1 form
if you keep the variables in your form code (NOT MODULE) it will never be global it stays public in the form itself
if i am on the wrong track then i'm not too sure what you are asking
Re: pass variable to form?
Quote:
Originally Posted by
Max187Boucher
if you keep the variables in your form code (NOT MODULE) it will never be global it stays public in the form itself
That's not entirely true - form's public variables and properties retain values even after form's been unloaded.
See quick sample below:
Code:
'form1 code
Option Explicit
Dim iTest As Integer
Public Test2 As Integer
Private Sub Form_Load()
Test1 = -1
Test2 = -1
Form2.Show
End Sub
Private Sub Form_Unload(Cancel As Integer)
Test1 = 99
Test2 = 88
End Sub
Public Property Get Test1() As Integer
Test1 = iTest
End Property
Public Property Let Test1(ByVal iNewValue As Integer)
iTest = iNewValue
End Property
'form2 code:
Option Explicit
Private Sub Command1_Click()
MsgBox "Form public property value: " & Form1.Test1
MsgBox "Form public variable value: " & Form1.Test2
End Sub
1. Run project: both public property (Test1) and public variable (Test2) get value of -1
2. Click button on Form2 to display their respective values (each should have -1_
3. Close Form1
4. Click button on Form2 again (you should get 2 msgbox's displaying 99 and 88)...
Re: pass variable to form?
I use collections in situations like this. they are a great help.
Re: pass variable to form?
Quote:
Originally Posted by
cprosser
..Each instance of FormB saves the value of a global variable that is used to index a global array of asset group info...
If the index is stored in a global variable then you can use it from any Form.
As alternative, when you need to pass an specific value to a Form you can used a public variable in that Form, then assign a value to it from the caller/Parent Form before Showing it. The best practice would be using a private variable that's used by a Property Get/Set in that Form and then using that property to assign/obtain the variable value and not the variable itself.
Re: pass variable to form?
Thanks to all who have taken the time to reply.
I considered using a global variable to specify the index to the global asset group info array for FormC. I thought this would not work because I was thinking that the user may want to add assets to more than one asset group at a time. Thinking this through more I realize that this is not necessary. I could set the global variable used to index the global asset group info array, then show FormC as a modal form.
Thanks again for your attention and helping me to think this through!
Re: pass variable to form?
You could set the tag property of formc and use that so long as you do not need it in the load event.