Consulting

Results 1 to 10 of 10

Thread: Use of Static Statement

  1. #1
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location

    Use of Static Statement

    Per VBA Help regarding the Static statement:

    Used at procedure level to declare variables and allocate storage space. Variables declared with the Static statement retain their values as long as the code is running.

    From this I would conclude that once the code stops running, variable values are "lost" and if the code is run again, static variables are reset to "zero". But that does not seem to be the case. Consider this test example:

    Sub TestStatic()
        Dim I As Integer
        Dim J As Integer
        For I = 1 To 5
            Call StaticSub(J)
        Next I
        MsgBox J
    End Sub
    Sub StaticSub(J)
        Static Counter As Integer
        Counter = Counter + 1
        J = Counter
    End Sub

    The first time I ran TestStatic, the display was "5". The second time it was "10" and so on. Counter is not resetting even though the code HAS stopped running. If I force an error leading to an abnormal end, the next time I run TestStatic, the display is again 5.

    So how do I force Counter back to zero each time I run the parent application?
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    I would do this.

    Option Explicit
     
    Dim Counter         As Long
     
    Sub TestStatic()
    Dim i               As Long
    Dim j               As Long
    Counter = 0
        For i = 1 To 5
            Call StaticSub(j)
        Next i
        MsgBox j
    End Sub
     
    Sub StaticSub(j)
    Counter = Counter + 1
        j = Counter
    End Sub

  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I've read that before in the Help files as well and found the same issue. The fact is that it will retain the value even though the code has stopped running. It should lose the value when the project is closed though.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Also if you click Reset button in the VBE the values are all reset.

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by DRJ
    I would do this.


    Option Explicit
     
    Dim Counter         As Long
     
    Sub TestStatic()
    Dim i               As Long
    Dim j               As Long
    Counter = 0
        For i = 1 To 5
            Call StaticSub(j)
        Next i
        MsgBox j
    End Sub
     
    Sub StaticSub(j)
    Counter = Counter + 1
        j = Counter
    End Sub
    Thanks for the idea. Unfortunately, Counter must be inside the child proc.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  6. #6
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by firefytr
    I've read that before in the Help files as well and found the same issue. The fact is that it will retain the value even though the code has stopped running. It should lose the value when the project is closed though.
    Thanks. Unfortunately, the parent procedure could be run many times before the project is closed.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  7. #7
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by DRJ
    Also if you click Reset button in the VBE the values are all reset.
    This is an interesting idea. Is there some way to click the VBE's Reset button programmatically?
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    You can use the End statement to clear all variables when you finish running ..

    Sub TestStatic() 
             Dim I As Integer 
             Dim J As Integer 
             For I = 1 To 5 
                     Call StaticSub(J) 
             Next I 
        MsgBox J 
        End
    End Sub 
    
    Sub StaticSub(J) 
        Static Counter As Integer 
        Counter = Counter + 1 
        J = Counter 
    End Sub
    Sorry about the layout - I don't know how to control the layout inside the VBA tag
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by MWE
    This is an interesting idea. Is there some way to click the VBE's Reset button programmatically?
    You could try

    Application.VBE.CommandBars.FindControl(ID:=228).Execute
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by TonyJollans
    You can use the End statement to clear all variables when you finish running ..

    Sub TestStatic() 
             Dim I As Integer 
             Dim J As Integer 
                   For I = 1 To 5 
                                Call StaticSub(J) 
         Next I 
         MsgBox J 
         End
    End Sub 
    
    Sub StaticSub(J) 
         Static Counter As Integer 
         Counter = Counter + 1 
         J = Counter 
    End Sub
    Sorry about the layout - I don't know how to control the layout inside the VBA tag
    Quote Originally Posted by xld

    ou could try

    Application.VBE.CommandBars.FindControl(ID:=228).Execute
    thanks to both of you. Both methods work.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •