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!

How to define public variable that keeps it's value 4

Status
Not open for further replies.

VBUser77

MIS
Jan 19, 2005
95
0
0
US
I like to define a public variable in my module that can be accessed from other modules within the same project but I also like my variable to retain its value (life).

Public MyVar as string (in the general section)
This way my variable is accessible from other modules but it doen't take its value. For some reason its value is set to zero when i try to use it in another module.

Any ideas how to overcome this problem. Thanks a lot.

Jay

 
declare your variable in the general declarations section of the module, right where you would normally declare "option explicit"

You could define the variable as either a true variable or a constant (const is the keyword if my memory is correct) if that is what you really want to declare.
 
Thanks but how could I keep the value of the variable live so that I could refer to the same value when i use it in other modules.

Using the static keyword is not helping me.



 
It will need to be a standard module, not a forms/reports module - in VBE - Insert | Module.

The public variable will then loose it's value at least under these circumstances:

* you get a run time error, and cancel it with the End button on the Debug message box
* usage of the End statement (End on it's own line, not End If, End Select...)

Else it should be available.

Roy-Vidar
 
You mean it loses its value at the End Function Statement? I am using a standard module and I like to preserve the value of my variable so that I could refer to it in other standard modules.

Thanks for your help

Jay
 
VBUser77,
why not store it in a table in the access db?
regards,
longhair
 
No, it looses the value when using the End statement (again not End If, End Select - also not End Sub, End Type, End Function, End Enum... - the End statement).

[tt] somevar=someothervar * 42
End ' < - the End statement[/tt]

- and - perhaps listen to longhair - usage of a table or two is a good precautin against loosing information...

Roy-Vidar
 
BTW - you are just saying that your public variables looses their values, you don't say anything about under which circumstances. I provided two possible circumstances for which you can trap - should you have any End statement, remove them, unless you really know what it is doing. The other thing is proper errorhandling, not allowing the debug thingie.

If those are met, your publics should behave "as usual" -> keep their value as long as the app is open... unless, of course, you have other code assigning "", 0 or whatever to this/these variables at points of the program where it shouldn't.

Roy-Vidar
 
VBUser77,
if you go the table route, just be careful of how you clean up the "global" vars (they really aren't global vars anymore). i would suggest resetting when ever the user restarts the app, this way if they loose the connection, blue screen, etc (aka shut the app in a way that was not programmed for) they won't have the same values if they should not. if they should you are just recreating the record in the table.
regards,
longhair
 
if you want a variable that keeps its value it is called a Contstant....

replace the parts with [] for your requirements

Code:
Global Const [myvar] As [myvartype] = [myval]

if you want a global variable that can be used as a master flag and updated from any module then you can define one like so
Code:
Global sDoUpd As String

I use the above flag that can be set from within any function / sub, I then check its value at the end and if still ok update the table with the changes.

Hope this helps

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top