Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Public Variables

Status
Not open for further replies.

spyder

Technical User
Jan 10, 2001
21
US
I don't understand this whole public variable thing. (New to programming and the VB book isn't clear).

If I create 4 forms, and I want one piece of info from each of those forms (a different piece of data from each) to appear in a 5th form, would I use a global variable?

For instance...form1 has a text box that a user would enter their name in, form2 has a test score that would calculate, I want both of these to appear in the form5 that would be designed as a Certificate and to replace a lbl.Caption property upon loading the form.

Is this when I would use a public variable? Where do I define this, on the form that first uses it? If I unload form1 and form2, would form5 still have received their data?

Please help.
 
Your best bet is to use a module - especially if you are planning to unload the forms (in which case any of their variables would go out of scope - ie.become "blank"). To do this in a module, just add a module to your project and declare your variable as public e.g.
public MyString as String
and then alocate its value in the code of the form where the variable resides e.g.
MyString = txtName.Text

There is another way to do this if you DON'T unload forms - you can refer to a variable on one form in the syntax of:
formname.variablename
but ONLY if the form is still loaded into memory. You can always hide the form and use its variables and then unload it when you are completely finished.

I have used both these methods - the module method works best if you are going to be referring to a variable in a variety of different places.

Hope this helps you

Good luck!!
 
Hi,

Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within theproject in which they reside

Private variables are available only to the module in which they are declared.

In your case you can declare both the name and score variables in a module as public. They will be available to all of your forms for as long as the project is open.

Have a good one!
BK
 
Thank you sooo much Craftor & BK. My program now works perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top