• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Scoop of a static variable

Hi the forum,
A variable is defined as Static in a main procedure. To use it in another procedure, I put the name as a parameter between the parentheses after the name of the procedure in both the call and the sub.
But when I try to use the static variable in a event procedure, VBA gives an error message.
The question is: is it possible to use a static variable defined in the main procedure in an event procedure as "Click" or "Change"?
Example
Code:
Sub Main()
Static num_quest as long
blabla
Call analyze(num_quest)
End sub
------------------
Sub analyze(num_quest)
blabla
End sub
-------------------
Sub combo_Change(num_quest) '<--------------not allowed

Could a member of the forum advise me to solve this problem?
Thanks in advance

Harry
 
Try this isnstead

Static num_quest as long
Sub Main()
...
End Sub
 
Hi Harry,

You can't change the signature of the combo_change() event handler by adding your own parameters.
Furthermore, Static variables can only have procedural level scope.

So, the direct answer to your question:
The question is: is it possible to use a static variable defined in the main procedure in an event procedure as "Click" or "Change"?
is no, you cannot.

One workaround is to use a module scope variable instead, as indicated by Hui, but without the Static keyword.

Declare your variable with Private scope at the module level. This means that the variable will be visible to anything within the same module, but hidden to anything outside the module. Module level variables retain their values between procedure calls in the same way as Static variables do, provided there isn't a state loss event.

Code:
Private num_quest As Long

Sub Main()
...
End Sub
 
Back
Top