• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Declaring a Variable Static for a Sub in a Form

I want to declare a static variable in a subroutine so it will have scope only for that subroutine. When the form is Unloaded I want the variable destroyed. I am using the Static Keyword like this: Static bAlreadyAdded as Boolean
and it remains static, but it remains static even through an Unload event because when I load the form again the value of bAlredyAdded is being retained.
I have also tried using the Static keyword in the Subroutine like this: Private Static Sub Form_Activate()
but the same thing occurs.

I want to maintain my code clean so I do not want to declare this in the general declarations section. Can someone offer guidance?


0
widner
Asked:
widner
  • 5
  • 5
  • 4
  • +1
1 Solution
 
mcriderCommented:
Use:

Private bAlreadyAdded as Boolean

and

Private Sub Form_Activate()

in your form.

Cheers!
0
 
widnerAuthor Commented:
MCrider,
My code looks like this:

Private Sub Form_Activate()
   Private bAlreadyAdded as Boolean
   If AlreadyAdded = False then
       'Do some work
       AlreadyAdded = True
   End if
End Sub

when I run the code I get the following error:
"Invalid Attribute in Sub or Function"

It doesn't like the Private in the variable declare.  I am using VB5 and do not have VB6 on this workstation to see if it works in VB6.  Is this the problem or am I missing something.  BTW thanks for the quick reply.
Kevin
0
 
mcriderCommented:
You need to do this:

Private Sub Form_Activate()
   Dim bAlreadyAdded as Boolean
   If AlreadyAdded = False then
       'Do some work
       AlreadyAdded = True
   End if
End Sub

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mcriderCommented:
But that will only keep bAlreadyAdded for the instanciation of Form_Activate..

If you want to use Private , you have to declare it in the forms General Declaration section as:

Private bAlreadyAdded as Boolean

Cheers!
0
 
twardCommented:
I think you want this:

Private Sub Form_Activate()
   Static bAlreadyAdded as Boolean
   If AlreadyAdded = False then
       'Do some work
       AlreadyAdded = True
   End if
End Sub

What you needed to change was the Private Static Sub Form_Activate() to
Private Sub Form_Activate().
0
 
widnerAuthor Commented:
Thanks but I need to keep the variable static for the life of the form.  So that would not work.  The way I read the Static keyword for a Sub it should work the way I want it to, but that is not the case.

Thanks again
0
 
twardCommented:
Another thing you can do is:

'(In General Declarations of the Form)'
Private lFirstLoad As Boolean

Private Sub Form_Load()
 
  lFirstLoad = True

End Sub

Private Sub Form_Activate()
 
  If lFirstLoad = True Then
   
    lFirstLoad=False

    ' Other Work '

  End If

End Sub
0
 
twardCommented:
If you just want to execute something in the Form_Activate Event only once then the above should work!
0
 
widnerAuthor Commented:
For Tward,
That is what I tried originally and the variable remained static even after the form unloaded.  I have tried:
Static Sub
dim bAlready

Private Sub
Static bAlready

Static Sub
Static bAlready

None do the trick.
0
 
Erick37Commented:
If you watch the Terminate event of your form, you will notice that it is not entered until you explicitly set the form to nothing.  Setting the form = nothing will clear your static variables.

Try this test.  If you comment out the Set Form2 = Nothing line, the terminate event is not entered, and the static variable remains in memory.

Private Sub Form_Terminate()
    Debug.Print "Terminate"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Form2 = Nothing ' Clears all the objects and statics
End Sub

Private Sub Timer1_Timer()
    Static tst As Integer
    Debug.Print tst
    tst = tst + 1
End Sub
0
 
twardCommented:
What is it you need to do with the variable?

Do you need the variable so that items in the Form_Activate Event are run only once?

You can make the variable Private to the Form, which means only the form can see it, but any function in the form can change it....
0
 
mcriderCommented:
Erick37 is right!
0
 
mcriderCommented:
However, you still need to declare the variable in the Declaration section and not the subroutine if you want the variable to be for the life of the form..

cheers!
0
 
Erick37Commented:
You can use the Static variable in a Sub, and it will remain for the life of the form, it just will not be available for use outside the Sub.

See my example using the Timer above.
It increments while the form is loaded.  If the form is set to nothing, the count starts at 0 on the next loading.  If you do not set the form to nothing, the count will resume with the last value it was assigned.

Why did you relock the question Mcrider?
0
 
widnerAuthor Commented:
Erick37 is correct and I do not want to declare the variable in the in the Declaration section.  It works correctly with it declared in the Sub.

thanks
0
 
Erick37Commented:
Try This:

Private Sub Form_Activate()
    Static bAlreadyAdded As Boolean
    Debug.Print "Activate"
    If Not bAlreadyAdded Then
        'Do some work
        Debug.Print "First Activate"
        bAlreadyAdded = True
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Form2 = Nothing
End Sub
0
 
widnerAuthor Commented:
Thanks for the help
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 5
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now