Solved

Pass values from form to form

Posted on 2000-03-22
31
159 Views
Last Modified: 2010-05-02
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
Comment
Question by:robhas
  • 10
  • 6
  • 3
  • +7
31 Comments
 

Expert Comment

by:MrOBrian
ID: 2646955
Why not use global variables declared in a module?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2646960
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
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 2647032
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
 
LVL 14

Expert Comment

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


Cheers!®©
0
 

Expert Comment

by:GinaP
ID: 2647078
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
 
LVL 18

Expert Comment

by:mdougan
ID: 2647120
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
 

Expert Comment

by:siewling
ID: 2647164
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2647189
seiwling, please reread the question.

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

A? "...when Form2 is loaded."
0
 

Expert Comment

by:siewling
ID: 2647823
Erick37,
Can't I add a statement like this "Form2.hide"?
0
 

Expert Comment

by:MrOBrian
ID: 2647856
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
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 2648328
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2648627
siewling,

If you post contains the phrases:

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

Post it as a comment not an answer...
0
 

Author Comment

by:robhas
ID: 2648798
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
 
LVL 5

Accepted Solution

by:
Bhaskar Ganapathe earned 100 total points
ID: 2648898
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2648901
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:mcrider
ID: 2648906
bhaspup,

Read the thread before posting to it...
0
 

Expert Comment

by:MrOBrian
ID: 2649845
Erick37 can have the points, his initial comment was more informative than mine, and I only beat him to it by 2 minutes :)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2650163
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2651248
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
 

Author Comment

by:robhas
ID: 2654872
Thanks Eric37 I will try that also
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2654947
You're welcome.
But why did you accept Bhaspup's answer?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2654986
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2655175
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2655310
wsh2... B.HASpup.cause??? Jeez! <LOL>
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2655329
Someone please interpret:

"Unlike accounts.. One question per thread, k?"
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2655425
It's just wsh2's warped (and I like it!) sense of humor... No big! <G>
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2655442
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2655488
Oh, <o> ~~~
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2655533
<ROTFLMAO!> Anybody got a newspaper? Roll it up and smack'm!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2655539
So to interpret...

robhas bhaspup

Rob has pups?? <G>
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2655544
Is that a stutter? <G>
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now