Results 1 to 37 of 37

Thread: reading a variable from a different form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Posts
    16

    Question reading a variable from a different form

    I have a question.. i have 2 forms.. and the first form loads and the user has to click on a control which assigns a value to a variable.. then the second form loads and the first one hides. i need the program to read the value from the variable declared in the first form from the SECOND FORM to use in the SECOND FORM.
    how can i do this?

    thanks to all in advance.

  2. #2
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    You have to declare a public variable in the first form and then, you just access it like so:

    VB Code:
    1. 'This is form one (Form1)
    2. Public txtDragValue As String
    3.  
    4. txtDragValue = "Jeremy is the man!"


    VB Code:
    1. 'This is form two (Form2)
    2. MsgBox Form1.txtDragValue

    HTH, Jeremy
    He who listens well, speaks well.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: reading a variable from a different form

    Originally posted by Jorgin
    I have a question.. i have 2 forms.. and the first form loads and the user has to click on a control which assigns a value to a variable.. then the second form loads and the first one hides. i need the program to read the value from the variable declared in the first form from the SECOND FORM to use in the SECOND FORM.
    how can i do this?

    thanks to all in advance.
    you must declare it as public.. then you can simply access it by using

    form1.variablename

  4. #4
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    If you are using a module you can make it Global or Public in the module and then you wont have to use the forms name..

    with form..\

    VB Code:
    1. ' in one form
    2. public X as integer
    3.  
    4.  
    5. ' some sub in another form or module
    6. form1.x = xxx

    with a module (The prefered way IMO)..

    VB Code:
    1. ' declarations
    2. Global X as integer
    3. 'or
    4. Public X as integer
    5.  
    6.  
    7. ' any place you want
    8. x = xx

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  5. #5
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    Globals

    But then you have a bunch of (unnecessary) global variables running around that don't need that level of scope. Dangerous as you can unintentionally change the values anywhere in your code.

  6. #6
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Globals

    Originally posted by Briantcva
    But then you have a bunch of (unnecessary) global variables running around that don't need that level of scope. Dangerous as you can unintentionally change the values anywhere in your code.
    They are NOT unnecessary if they are needed. The purpose of making them Global or Public in a module is so that they can be easiely access from anywhere in your code. like what is needed here. We have two forms sharing a variable. Make it Global and both hae equal access to it, and both will effect it globaly. Just like if you made it public within a form. The big difference here is that by making it Global/Public in a module you will not have to have both forms loaded all the time. If you make it public in a form, you will have to have form1 loaded for form2 to have access to the variable, Thus taking up memory...


    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  7. #7
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    I respectfully disagree

    The original problem was how to pass a variable from Form1 to Form2. Form2 is the only variable consumer. Thus the variable only needs to exist within the scope of Form2. Were this variable needed for the whole project and multiple forms needed access and manipulation, then a global would be appropriate.

    And yeah, I do realize we're arguing and vs. also.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    The best way to handle the situation is to add a property to the first form using code like the following:

    VB Code:
    1. Option Explicit
    2. Private mintValue As Integer
    3. Public Property Get MyProperty() As Integer
    4.  
    5.     MyProperty = mintValue
    6.    
    7. End Property
    8.  
    9. Public Property Let MyProperty(ByVal intNewVal As Integer)
    10.  
    11.     mintValue = intNewVal
    12.  
    13. End Property

    Usage in Form1
    VB Code:
    1. MyProperty = ValueToBeAssigned

    Usage in Form2
    VB Code:
    1. ThevalueIs = Form1.MyProperty

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Martin,
    what would the benefit of having a let/get and private variable in the form versus just a public variable if you aren't doing anything in the let/get statements?

  10. #10
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    There are many ways to skin a cat, we all know this, but why make it any harder than it has to be? My first idea is the most simple, working example yet. USE IT!
    He who listens well, speaks well.

  11. #11
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: I respectfully disagree

    Originally posted by Briantcva
    The original problem was how to pass a variable from Form1 to Form2. Form2 is the only variable consumer. Thus the variable only needs to exist within the scope of Form2. Were this variable needed for the whole project and multiple forms needed access and manipulation, then a global would be appropriate.

    And yeah, I do realize we're arguing and vs. also.
    I do agree with you, but want to add something. I try to code for the bigger picture. If two forms are going to need it, then the odds are that more will eventually and the code will have to be re-written, but I also keep in mind that that is not always the case. It will be up to the coder to make that decision..

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  12. #12
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Very well said.
    He who listens well, speaks well.

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by kleinma
    Martin,
    what would the benefit of having a let/get and private variable in the form versus just a public variable if you aren't doing anything in the let/get statements?
    • It's more flexible
    • He's not doing anything now, but maybe later he'd like to
    • It allows for validation

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by JCScoobyRS
    There are many ways to skin a cat, we all know this, but why make it any harder than it has to be? My first idea is the most simple, working example yet. USE IT!
    Yes your way is the simplest and there's nothing wrong with it by itself, but once you start throwing in public variables you can wind up with a mess unless you are careful.

  15. #15
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Once again...I agree. No one is wrong here. Isn't that great. WOOHOO!!!
    He who listens well, speaks well.

  16. #16
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416

    Re: Re: Globals

    Originally posted by RudyL
    They are NOT unnecessary if they are needed. The purpose of making them Global or Public in a module is so that they can be easiely access from anywhere in your code. like what is needed here. We have two forms sharing a variable. Make it Global and both hae equal access to it, and both will effect it globaly. Just like if you made it public within a form. The big difference here is that by making it Global/Public in a module you will not have to have both forms loaded all the time. If you make it public in a form, you will have to have form1 loaded for form2 to have access to the variable, Thus taking up memory...


    Rudy
    that kind of defeats the purpose of object oriented programming though...... thats back to the old structured programming ways.

    although global vars are needed at some points..... i think in this case its best to have it at the form-level. (public var within a form class)

  17. #17
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Re: Re: Re: Globals

    Originally posted by Eras3r
    that kind of defeats the purpose of object oriented programming though...... thats back to the old structured programming ways.

    although global vars are needed at some points..... i think in this case its best to have it at the form-level. (public var within a form class)
    I can see your point about object oriented programming, but if all you need is the variable, then why create the object?

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  18. #18
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    Re: Re: Re: Re: Globals

    Originally posted by RudyL
    I can see your point about object oriented programming, but if all you need is the variable, then why create the object?
    B/c the variable only exists within, and b/c of, the (form) object - it's unnecessary outside of it. To use your memory example from before, why allocate storage space for the lifetime of a project (a global) when it's (the variable) only needed in two froms which always talk to one another (i.e, form2 only exists b/c form1 called it)? For the same reaon we don't dim longs when we only need a byte, or don't declare everything as variants.

  19. #19
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    he wasnt making a form just to hold a variable... i assumed that form was a functional part of his program and had purpose.

    making a form to hold variables wouldnt make any sense... because you just just stick the variable in the form your actually using... lol?

  20. #20
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Originally posted by Eras3r
    he wasnt making a form just to hold a variable... i assumed that form was a functional part of his program and had purpose.

    making a form to hold variables wouldnt make any sense... because you just just stick the variable in the form your actually using... lol?
    That isn't what I ment..

    Lets say you have a variable called X. You create it in form1 and use it for stuff like counters in "while loops" or "for next" statement.. (For example)

    Now you have a new form, form2. In that form you have loop as well so you use X as your counter. Well, in order to do this you need to use "form1.x = ", thus causing form1 to load. Now form1 may already be loaded, but still a bad practice. You should have a module for variables like that.

    Now I will say that I might be over analyzing the scope here. In this case, assuming (bad thing to do) that form2 is only driven by form1, then I agree with using a variable in form1 for form2. But unless you release the form from memory when you are done with it (The form, not the program, big difference) you are still holding that variable in memory, thus having no different effect on the code (memory ways) than creating a global/public variable. But you will have the annoying task of calling form1.x everytime for no reason...

    I guess the moral is, make sure you release your form form memory to make using a local variable to form usefull..

    Make sense??

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  21. #21
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Jorgin,
    Can you please tell us if your issue has been resolved? Later, Jeremy
    He who listens well, speaks well.

  22. #22
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    for what my 2 cents is worth:

    I dont see what all the panic is about Public variables in modules. I dont mind using them when needed. It certainly makes more sense to me than passing paramaters around all over the place when a simple public declaration would suffice. I can see how they can be dangerous, but so can 90% of everything else we use in VB.

  23. #23
    New Member
    Join Date
    Apr 2003
    Location
    Benicia, CA
    Posts
    12

    Question Passing array between forms

    I'm doing something similar, only I'm using a dynamic array. I'm trying to write an app to add a list of users to a group. As

    I've got it now, the array is declared and utilized only in Form2 and Form3 (it prints it's contents to a ListBox in 3).

    However, I want Form1 to utilize the array once it's been populated, and unload Form2&3.

    As I understand this, I need to move the declaration of UserArray() out of the subroutine on Form2 and make it a general

    declaration as a public array on Form1? What happens to the array when Form2 gets unloaded? Will the array still be populated

    with the data provided by Form2?

    VB Code:
    1. 'This is Form1 as it stands
    2. Private Sub AddMember_Click()
    3.     Dim Group As IADsGroup
    4.     Dim GroupName As String
    5.     Dim GroupDomain As String
    6.     Dim User As IADsUser
    7.     Dim UserName As String
    8.     Dim UserDomain As String
    9.     Dim Prompt As String
    10.     Prompt = "Please enter the target group."
    11.     GroupName = InputBox(Prompt)
    12.     GroupDomain = "TestDomain"
    13.     UserName = "target_user_name"   'This is gonna get changed to UserArray()
    14.     UserDomain = "LSNA"
    15.    
    16.     'Obviously this next part will have to be changed to utilize an array.
    17.     Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
    18.     Set Group = GetObject("WinNT://" & GroupDomain & "/" & GroupName & ",group")
    19.     Group.Add (User.ADsPath)
    20.     Group.SetInfo
    21.    
    22. End Sub
    23.  
    24. 'This is the main subroutine of Form2 as it stands
    25. Private Sub File1_Click()
    26.     Dim UserList
    27.     Dim UserArray() As String
    28.     Dim UserCounter As Integer
    29.     Dim X As Integer
    30.            
    31.     Set Fso = CreateObject("Scripting.FileSystemObject")
    32.     SelectedFile = File1.Path & "\" & File1.FileName
    33.     Set UserList = Fso.OpenTextFile(SelectedFile, 1)
    34.    
    35.     UserCounter = 0
    36.     ReDim UserArray(UserCounter + 1)
    37.     Do Until UserList.AtEndOfLine = True
    38.         ReDim Preserve UserArray(UBound(UserArray) + 1)
    39.         UserArray(UserCounter) = UserList.ReadLine
    40.         UserCounter = UserCounter + 1
    41.         MsgBox (UserCounter)
    42.     Loop
    43.    
    44.     Load Form3
    45.  
    46.     For X = 0 To (UserCounter - 1)
    47.         Form3.List1.AddItem UserArray(X)
    48.     Next X
    49.    
    50.     Form3.Show 1
    51.  
    52. End Sub

  24. #24
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    If the array is declared within a form, general or in a sub it will be destroyed (removed from memory) when the form is unloaded or even if it is "reloaded".


    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  25. #25
    New Member
    Join Date
    Apr 2003
    Location
    Benicia, CA
    Posts
    12
    What I'm thinking is I declare the dynamic array as being public in Form1. Then Form2 loads, populates the array and then gets unloaded, returning back to Form1. Since Form1 was open the entire time, will the array remain in tact or will it get busted up when Form2 closes?

  26. #26
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    As long as form1 is still loaded it will be fine.
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  27. #27
    New Member
    Join Date
    Apr 2003
    Location
    Benicia, CA
    Posts
    12
    Hmmm... I've hit a new snag . At the top of Form1, I've created the following general declaration:

    VB Code:
    1. Public UserArray() As String
    When I test the app, I immediately get a compile error that says:

    Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object models
    I haven't been doing this long enough to understand why it's not working

  28. #28
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127
    That's because you can't have public arrays. You can create an array on one form and then set public regular variables to the part of an array that you want to transfer to another form.

    Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules.

    Which means "Arrays are not allowed as Public members of object modules."

    Try creating a BAS module and put that syntax in that BAS module, then try it.

    ~B-Rabbit

  29. #29
    New Member
    Join Date
    Apr 2003
    Location
    Benicia, CA
    Posts
    12
    Sorry, I don't follow. So what I should do is declare the array in the subroutine of Form1, then call on it form Form2 so that it can be populated? How do I do that?

    Thanks for the help. I greatly appreciate it.

  30. #30
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127
    No, you create a module by going to Project -> Add Module. Then, in that module, you can do type Public UserArray() As String.

  31. #31
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by Keeper
    Sorry, I don't follow. So what I should do is declare the array in the subroutine of Form1, then call on it form Form2 so that it can be populated? How do I do that?

    Thanks for the help. I greatly appreciate it.
    As B-Rabbit said, you can't have a public array in a form. That's because Arrays cannot expose thier properties as public members of a class. A from is just a module of the form class. Arrays can't be public members of class modules either.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  32. #32
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Originally posted by RudyL
    If the array is declared within a form, general or in a sub it will be destroyed (removed from memory) when the form is unloaded or even if it is "reloaded".

    Rudy
    All Form Variables are still there in memory even if you "unload" the form. All that happens when you "unload" a form is the visual components are gone, but every variable that form had is still in memory. Unless you set the form to = Nothing you can unload the form the load it again and still get your Variable values...
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  33. #33
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127
    How? Can you give an example?

  34. #34
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Originally posted by B-Rabbit
    How? Can you give an example?
    Form1:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Form2.Show
    5. End Sub
    Form2:
    VB Code:
    1. Option Explicit
    2.  
    3. Public SomeVar As Long
    4.  
    5. Private Sub Command1_Click()
    6.   SomeVar = SomeVar + 1
    7.   Caption = SomeVar
    8. End Sub
    9.  
    10. Private Sub Form_Load()
    11.   Caption = SomeVar
    12. End Sub
    Click the button on Form1 to load Form2, then click the button on Form2 to increment the counter.
    Close Form2, unloading it, then Click the Button on Form1 to reload Form2, notice the variable is still set to it's last value.

  35. #35
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    2 forms

    Form 1 has 1 Public Variable called Test as String declared in the general section of the form

    Form 1 has 2 buttons

    Button1 has code that gives Test a value and unloads form1 and shows Form 2.

    Button2 has code that has a msgbox show the value of Test.


    Form 2 has 1 button

    Button1 loads Form 1

    When Form 1 is loaded press button2 on Form 1 and it will display the value of the Variable Test that you gave it before you unloaded Form1.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  36. #36
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127
    I knew that, I thought you meant when your app unloads and there are no more forms, you can still retrieve variables, although you can't.

  37. #37
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Originally posted by B-Rabbit
    I knew that, I thought you meant when your app unloads and there are no more forms, you can still retrieve variables, although you can't.
    I don't know why you would have thought that... i never mentioned the word app at all. I specifically said when you "unload" a "form"..

    And if you read you will see that i was replying directly to someone who was saying you can't retrieve variables from "forms" that have been unload/loaded..
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


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