• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Pass values from form to form

A form holds two values which I would like to pass to other forms. I currently reference the form with the values by keeping the form hidden. Is there a better solution instead of keeping a form open. I thought about using a class but just confused myself even more.
Thanks
Bob
0
robhas
Asked:
robhas
  • 10
  • 6
  • 3
  • +7
1 Solution
 
MrOBrianCommented:
Why not use global variables declared in a module?
0
 
Erick37Commented:
You can declare the variables as Global in a Module, then they are available to all forms all the time.

'In a module
Global glValue As Long
0
 
Glen RichmondCommented:
classes are easy to understand just think of them as anyother object like a text box, they have properties and methods that you define..

if you imagine a radio, a  propertie would be its display and a method a button to change channel..

a global is the easiest way to store a value to use in your project but you could use a class.

hope this helps a little. if you want a example ill be happy to help just post a comment...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mcriderCommented:
Gotta go with MrOBrian & Erick37 on this one... Global definition in a Module is definately the easiest way to go...


Cheers!®©
0
 
GinaPCommented:
Let's say you passing values from Form1 to Form2.
1.After Option Explicit (declaration section) of Form2t:

Public Variable1 as string(or ...)
Public Varialbe2 as integer(or ...)

2. In the  Form1 when you have values:
 
 Form2.Variable1 = YourValue1
 Form2.Variable2 = YourValue2

Now Form2 received the values.



0
 
mdouganCommented:
There would probably be a little less overhead but I think of a form as a kind of class.  The thing is that if you never need to see it, you should just use a class.  It's easy.  Just add a class module to your project.  Give it a name (say MyClass) then, in your code instead of saying :

Load frmHidden
frmHidden.MyVar = 100
frmHidden.Unload

Say Dim x as MyClass
Set x = New MyClass
x.MyVar = 100
Set x = Nothing

Make sure that MyVar is defined as Public in your class.
0
 
siewlingCommented:
robhas,
      Does the mentioned values mean 'the values in textboxes'? If they are, then we can refer them as "Form2.Text1 = Form1.Text1" when Form2 is loaded.
0
 
Erick37Commented:
seiwling, please reread the question.

Q: "Is there a better solution instead of keeping a form open."

A? "...when Form2 is loaded."
0
 
siewlingCommented:
Erick37,
Can't I add a statement like this "Form2.hide"?
0
 
MrOBrianCommented:
Well, if I may jump in here...  just hiding a form leaves it in memory, and I believe what robhas was looking for was a way to remove that form from memory completely, but still be able to use the variables.  The easiest, and cheapest as far as memory, way to do it is to declare a couple of global variables in a module, then you could unload the previous form, freeing up the memory, and still be able to access those variables from anywhere in your program.
0
 
Glen RichmondCommented:
siewling i think the point Erick37 is making is that you have locked this question with an answer when people have offerd more applicable answers as comments before you. not locking questions makes for a productive exchange of views. now its locked the question will only appear in the locked area of the site, this area is usually ignored by most experts.
0
 
mcriderCommented:
siewling,

If you post contains the phrases:

   "I think..."
   "How about..."
   "If (something)..."

Post it as a comment not an answer...
0
 
robhasAuthor Commented:
Did not read the question in detail. I wanted info to unload form and retain values.
If MrOBrian or Eric37 would post an answer I would accept it. Thank you all for the help, I don't know why I did not think of Global Variables.
Thanks
Bob
0
 
Bhaskar GanapatheCommented:
Hi robhas,

   Add a module and declare 2 variables as
   Global var1 as <datatype>
   Global var2 as <datatype>

   When unloading a form assign the values to these global variables.

   Then use this variables in any form as you like..

   Ok...

 

0
 
mcriderCommented:
robhas,

You can just select one of their comments as the answer by clicking on the "Accept Comment..." button on the right side of the comment...


Cheers!®©
0
 
mcriderCommented:
bhaspup,

Read the thread before posting to it...
0
 
MrOBrianCommented:
Erick37 can have the points, his initial comment was more informative than mine, and I only beat him to it by 2 minutes :)
0
 
Erick37Commented:
One last comment.

Global variables are easy to implement but hard to manage.  That is, they are not logically grouped to a particular form or object.  If the variable you want to be exposed to other forms is somehow related to (eg) Form2, it makes sense to keep the variable in Form2's code.

If you declare the variable as public in Form2, you can access this variable without loading Form2.

Try this in Form2:

Public plVal as Long


From Form1 call:

Private Sub Command1_Click()
    Dim frm As Form
    Form2.plVal = 100
    Debug.Print Form2.plVal
    For Each frm In Forms
        Debug.Print frm.Name & " is loaded"
    Next
End Sub

Note that Form2's Load event is not fired and that it is not listed as a loaded form.

Even if you call
Unload Form2
the Public values will remain intact.  The only way to wipe them out is to call
Set Form2 = Nothing

This is just an observation, I have not read anyting documenting this.  It seems that the compiler treats all Public variables as static.
VB5 SP3
0
 
wsh2Commented:
With Globals.. I always create a BAS Module (VB Menu.. Project.. Add Module)and then name it GLOBALS.  Additionally, I prefix each global name with a g_ (eg. Public g_intVariableName as Integer).  In doing this, in my program, I can always tell if a variable is a global by its prefix, know that it is in a BAS Module named GLOBALS.. and know that it is in scope for any/everything in the project.

0
 
robhasAuthor Commented:
Thanks Eric37 I will try that also
0
 
Erick37Commented:
You're welcome.
But why did you accept Bhaspup's answer?
0
 
mcriderCommented:
I think robhas accepted it by mistake...
Wouldn't it be nice if robhas could undo that grade?  I've got an open suggestion at http://www.experts-exchange.com/EQ.10313682 

Maybe you could go over there and comment...
0
 
wsh2Commented:
Erick37:
Unlike accounts.. One question per thread, k?.  roBHAS accepted the answer B.HASpup.cause he wanted too.. so there.. <lol> and a <wink>.
0
 
mcriderCommented:
wsh2... B.HASpup.cause??? Jeez! <LOL>
0
 
Erick37Commented:
Someone please interpret:

"Unlike accounts.. One question per thread, k?"
0
 
mcriderCommented:
It's just wsh2's warped (and I like it!) sense of humor... No big! <G>
0
 
wsh2Commented:
Erick37:
Me-thinks roBHAS has a dual personality.. and dat da BHASpup.. has yet to be paper trained.. (better check there isn't anything on the bottom of your shoes).. <lol> and a <wink>.
0
 
Erick37Commented:
Oh, <o> ~~~
0
 
mcriderCommented:
<ROTFLMAO!> Anybody got a newspaper? Roll it up and smack'm!
0
 
mcriderCommented:
So to interpret...

robhas bhaspup

Rob has pups?? <G>
0
 
mcriderCommented:
Is that a stutter? <G>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 6
  • 3
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now