-
Aug 4th, 2012, 11:33 PM
#1
Thread Starter
New Member
pass variable to form?
Hello,
I am using VB 6 to create a program that will allow a user to create and manage groups of assets.
FormA instantiates multiple instances of FormB, one per user menu click. FormB is used to create and manage a group of assets. Each instance of FormB saves the value of a global variable that is used to index a global array of asset group info. So each instantiation of FormB stores assets in one element of the array of asset group info.
I would like each instantiation of FormB to display FormC on user menu click, to add an asset to the asset group associated with that instantiation of FormB. How can I get FormC to make assignment to the correct asset group info array element? Is there any way for an instance of FormB to pass the saved asset group info array index to FormC?
I apologize if this description is not clear. I'm relatively new to Visual Basic programming. Perhaps there is another way to solve this problem. I appreciate any assistance.
Thanks,
Charles
-
Aug 5th, 2012, 12:15 AM
#2
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
-
Aug 5th, 2012, 01:10 PM
#3
Re: pass variable to form?
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)...
-
Aug 5th, 2012, 03:02 PM
#4
Lively Member
Re: pass variable to form?
I use collections in situations like this. they are a great help.
-
Aug 5th, 2012, 06:37 PM
#5
Re: pass variable to form?
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.
-
Aug 6th, 2012, 10:16 PM
#6
Thread Starter
New Member
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!
-
Aug 7th, 2012, 12:07 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|