Results 1 to 7 of 7

Thread: pass variable to form?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    5

    Question 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

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    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

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: pass variable to form?

    Quote Originally Posted by Max187Boucher View Post
    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)...

  4. #4
    Lively Member
    Join Date
    Oct 2011
    Posts
    126

    Re: pass variable to form?

    I use collections in situations like this. they are a great help.

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: pass variable to form?

    Quote Originally Posted by cprosser View Post
    ..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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    5

    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!

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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
  •  



Click Here to Expand Forum to Full Width