Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Public Variable (access2000)

    Hi,

    how do i declare a Static variable that can be used by more than one sub procedure in the formatting of a report?

    i have a GroopFooter1_Format sub that needs to update a static variable but the ReportFooter_Format sub needs to do a calculation with that value.

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Variable (access2000)

    Hans, thanks for that, i didnt realise that this sort of variable would have static properties.

    i have also found the the FormatCount variable that comes along with the _Format procedure allows me to do the same thing.

    thanks again

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public Variable (access2000)

    Declare the variable at the top of the report module (before all Subs and Functions), not within a Sub or Procedure. That way, every routine in the report module can access the variable. You don't need the Static keyword for this, just Private varname As ...

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Public Variable (access2000)

    Don't get module level and static variables confused. They aren't the same thing at all. A module level variable is available to every part of the form or report, a static variable holds its value between calls within the same session of Access.

    The FormatCount argument tells you whether this is the first time a section has been formatted or a subsequent pass. A section may be formatted more than once depending on how long the report is, how long the section is, whether you set any groupings like Keep together with Detail, and whether you put something like "Page 1 of 95" in the page footer. That is absolutely the *only* thing it tells you, so don't try to use it for anything else.
    Charlotte

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Variable (access2000)

    thanks for this Charlotte, i do admit to a little confusion on this.
    Is there such a thing as a module level , static variable ?

    the FormatCount does what i need it to - a calculation and updates to some text boxes on the first pass through the report. I was going to use a static variable to count my way through and only make the calculation at the right time but FormatCount does that for me.

    thanks

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public Variable (access2000)

    A Static variable is defined within a procedure (Sub or Function). Ordinarily, a local variable (a variable defined within a procedure) only "lives" as long as the procedure runs; it is created anew each time the procedure is executed. A static variable remains in memory (and keeps its value) as long as the module that contains the procedure. A static variable, like any local variable, is available within the procedure only. If you try to refer to a static variable outside its own procedure, you get a syntax error (assuming you have Option Explicit set.)

    A module-level variable is set at the top of a module, before all procedures. It will "live" (and keep its value) as long as the module is in memory. If you declare it as Private, it will be available to all procedures within the module, but not to procedures in other modules. If you declare it as Public, it is available to procedures in other modules too.

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Cornwall, England
    Posts
    393
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Variable (access2000)

    I have that firmly in mind now, how does a module level static variable get created ?

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public Variable (access2000)

    I'm sorry, apparently I haven't been clear enough. The keyword Static can't be used at the module level, it is only valid within a procedure. But a variable defined at the module level acts much like a static variable - once you assign it a value, it keeps that value until you assign another value.

    Let's look at an example. Assume that you create a standard module with the following contents

    <img src=/w3timages/blueline.gif width=33% height=2>

    Option Compare Database
    Option Explicit

    Public lngNumber1 As Long ' Define a module-level public variable
    Private lngNumber2 As Long ' Define a module-level private variable

    Private Sub Test1()
    Static lngNumber3 As Long ' Define a local static variable
    Dim lngNumber4 As Long ' Define a local non-static variable

    lngNumber1 = lngNumber1 + 1
    lngNumber2 = lngNumber2 + 1
    lngNumber3 = lngNumber3 + 1
    lngNumber4 = lngNumber4 + 1
    MsgBox lngNumber1 & " - " & lngNumber2 & " - " & lngNumber3 & " - " & lngNumber4
    End Sub

    Private Sub Test2()
    lngNumber1 = lngNumber1 + 1
    lngNumber2 = lngNumber2 + 1
    lngNumber3 = lngNumber3 + 1 ' Causes an error!
    lngNumber4 = lngNumber4 + 1 ' Causes an error!
    End Sub

    <img src=/w3timages/blueline.gif width=33% height=2>

    lngNumber1 is a public module-level variable. It is available in all procedures in all modules. The value of lngNumber1 is increased each time you run Test1.

    lngNumber2 is a private module-level variable. It is available in all procedures within this module, but it is unknown to other modules. It you try to refer to it in another module, you'll get an error message. The value of lngNumber2 is increased each time you run Test1.

    lngNumber3 is a static local (procedure-level) variable. It can be used in Test1, and it will keep its value between runs, so its value is increased each time you run Test1. Referring to lngNumber3 in Test2 causes an error, since it is local to Test1.

    lngNumber4 is a non-static local (procedure-level) variable. It is created anew each time you run Test1, so the MsgBox function will display 1 each time. Referring to lngNumber4 in Test2 causes an error, since it is local to Test1.

    Both Test1 and Test2 are private procedures. That is, they can be called from within the module, but not from another module. If you use Public instead of Private, they can be called from any module.

Posting Permissions

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