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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variables

Status
Not open for further replies.

lcs226

Technical User
Apr 22, 2008
60
US
Hello All.

I am having an issue with global variables that are declared in the module not retaining there values from execution of a routine from 1 time too the next. Should I write the value to an Excel file for memory or what?

Thanks.
 


Hi,

What do you mean by, "from 1 time too the next?"

Does that mean in the same session of your application or from one session to another?

AND what application are you referring to?

Please answer ALL questions.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip, To answer your questions,

1, from one session to another. I will execute a sub-routine and write values to the variables, then go to reuse them with the execution of another sub routine at a later time (minutes to hours later). This is without shutting down the application taht is running the VBA.

2, The application I am using is RSView32
 



And how are you declaring your global variables?

Also, notice that is forum refers to Microsoft VBA.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Module level variables should be maintained as long as the module; explicit resets (e.g. End statements) will re-initialise them but, otherwise, the values should be retained. I don't know RSView32 - perhaps it has some mechanism (deliberate or accidental) that clears things out under certain conditions, but I would think it more likely that some wayward code is doing it.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Variables are declared as follows

Public TapID As Integer
Public TimeStart As String
Public Time_StartOfTap As String
Public Time_EndOfTap As String
Public KWHStart As Integer
Public KWH_StartOfTap As Integer
Public KWH_EndOfTap As Integer
Public KWHold As Integer

And RSView32 has Microsft VBA imbedded in it.
 


and you have these declarations BEFORE your FIRST PROCEDURE?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I think this is a case for some good old fashioned debugging. You need to ascertain the circumstances under which this problem occurs - try to note what happens between the time you know the variables to be good, and the time you find them missing. It really isn't possible at a distance - that said, do you have any Event processing that might be interfering at all?



Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Here is a shot in the dark, but I have done this a few times and I have seen people do this on this site.

Assume I start programming and I make a sub routine.

public someSub()
dim TapID As Integer
dim TimeStart As String
...
end sub

Then later I determine that I really need to make these global
so I
Public TapID As Integer
Public TimeStart As String
but I forget to take the variable declaration out of the subroutine and I never assign any value in the subroutine.

You can have local and global variables of the same name.

Then if I set the value of my public variable somewhere, but check it in the subroutine it appears that it has no value. That is because it will return a reference to the local TapID not the public TapID.

I have done this a few times, and sometimes you just do not see it right away. It will give similar symptoms to what you explain.
 
this demo shows this
Code:
Public bothLocalAndGlobal As String

Public Sub Test()
 SubA
 SubB
End Sub

Public Sub SubA()
  bothLocalAndGlobal = "ABC"
  MsgBox "SubA: " & bothLocalAndGlobal
End Sub

Public Sub SubB()
 Dim bothLocalAndGlobal As String
 'later I decide this variable should be global
 'but I forget to take out the local declaration
 MsgBox "Sub B: " & bothLocalAndGlobal
End Sub
It appears that in sub B my variable has lost its value, but it is returning the value of the local variable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top