Related Information Examples & Tutorials

How To Use Static Variables

*** THIS IS AN ADVANCED TOPIC ***

This topic discusses the use of static variables in reports. Static variables are commonly used to calculate running totals.

Requirements

Understanding of and experience using the Report Writer in Collect!

Read How To Use Variables. Please refer to it for the basics before applying them to static variables.

If you are using static variables to calculate totals, please refer to How to Assign and Calculate Using Variables.

Top of page.

Overview

When using variables in multiple looping procedures, you may find that they do not behave as you expect. If you want a value to be under your full control, declare it as 'static.' Then no procedure will change its value in an unexpected way or reset it to zero.

Static variables are declared and assigned just as non-static variables, except that they are prefixed with @tvar instead of @var. The basic formatting principles described in How to Use Variables apply to them as well.

tip.gif It is advisable to test the output of your report, thoroughly, especially when using variables in calculations. You may find that you need to adjust the placement of the variable in your looping procedures to produce the desired results.

Top of page.

Totaling Values With Static Variables

It is important to understand the functionality of variables before attempting to total values using static variables. If you are unfamiliar with what a variable is, please review How To Use Variables before proceeding.

First we need to decide what we will be totaling. For our purposes we will be creating a snippet of code that totals all the Principals listed in your database.

When using variables, we always need to declare what type of variable we want, as well as the default value it will hold. We will want our variable to default to 0.00 since we want to begin adding from 0.00 to get an accurate total. We will also want this variable to be a currency type of variable.

We now know all the information we need to declare our variable. In this example, we are going to use the name @tvarTotal.

tip.gif You may name your variables anything you like, however they must start with a @tvar for a static variable or @var for a regular variable.

Our declared static variable will be in a debtor loop and will look like the following example.

@tvarTotal$ = 0.00 // Declare total variable
@de no total // Beginning of debtor loop
@de.na<30> @de.fi @de.pr<13.2> // Display the Debtor Name, File Number, and Principal
@de // End of debtor loop

If you are unfamiliar with loops please review the Help topic How To Use Loops.

The next step is to determine where we should position our static variable so we can keep a running total of the desired value. In our example, we want to add up all the Principals listed for our debtors. The logical choice is to position it within the debtor loop.

Next, we need to determine which field value we want to add to our @tvarTotal variable. Since the Principal field is @de.pr this will be the field we will add to our variable. Our report snippet should now look like the following example.

@tvarTotal$ = 0.00 // Declare total variable
@de no total // Beginning of debtor loop
@de.na<30> @de.fi @de.pr<13.2> // Display the Debtor Name, File Number, and Principal
@tvarTotal = @(tvarTotal+de.pr) // Adds the principal amount to our total variable
@de // End of debtor loop

Now that we have the variable populating with the values of all the Principals listed in our database, we want to print it out upon completion. This can be achieved by typing the variable name where we want the total displayed. Since we want the value printed once it is done totaling, we will print the variable after the end of the debtor loop. Our report snippet should now look like the following example.

@tvarTotal$ = 0.00 // Declare total variable
@de no total // Beginning of debtor loop
@de.na<30> @de.fi @de.pr<13.2> // Display the Debtor Name, File Number, and Principal
@tvarTotal = @(tvarTotal+de.pr) // Adds the principal amount to our total variable
@de // End of debtor loop
Total Principal Listed: @tvarTotal< // Displays the total along with the heading Total Principal Listed

Top of page.

Counting Records With A Static Variable

// This snippet uses a counter to count the number of
// debtors listed as well as totals the owing.

@tvarOwing$ = 0.00
@tvarCounter# = 0

@de
@tvarCounter>5> @de.na<30> @de.fi @de.ow>13.2>
@tvarOwing = @(tvarOwing+de.ow)
@tvarCounter = @(tvarCounter+1)
@de
Debtors Listed: @tvarCounter<5>
Total Owing: @tvarOwing<13.2>

@tvarCounter retains the total number counted in the
loop above. It can be referenced later in the report, if needed.

This is the output from the above coding.

Debtor File# Amount

--------------------------------------------------------------------------------

1 Long, Anna 4356 $8,622.34
2 Gough, James T. 4342 $6,390.28
3 Boag, Peter 1425 $3,859.33
4 Wilson, Jonathon 4323 $725.00

Debtors Listed: 4
Total Owing: $19,596.95

Notice that the counter is incremented by 1 after each line is printed. Placing >5> after @tvarCounter positions the numbers attractively, right justified.

Top of page.

Date Range With Static Variables

This example shows how you might use static variables to display debtor accounts where payment is overdue. This example reads the Debtor's Next field, @de.ne and looks for values in several ranges.

Declaring the date variables:

@tvarOver30! = 01/01/1960
@tvarOver60! = 01/01/1960
@tvarOver90! = 01/01/1960
@tvarOver120! = 01/01/1960

Assigning values to the variables:

@tvarOver30 = @d-30
@tvarOver60 = @d-60
@tvarOver90 = @d-90
@tvarOver120 = @d-120

If you are unfamiliar with the date and time codes please review How To Use Date And Time Codes in the Help topics.

Using the variables in where clauses with ranges:

Accounts within 30 days:

@de no total where (@de.ne < @tvarOver30)
Your data fields here
@de

30 to 60 days overdue:

@de no total where (@de.ne = @tvarOver60 .. @tvarOver30)
Your data fields here
@de

60 to 90 days overdue:

@de no total where (@de.ne = @tvarOver90 .. @tvarOver60)
Your data fields here
@de

90 to 120 days overdue:

@de no total where (@de.ne = @tvarOver120 .. @tvarOver90)
Your data fields here
@de

Over 120 days overdue:

@de no total where (@de.ne > @tvarOver120)
Your data fields here
@de

In this example, the order of the variables is important in the ranges.

For instance, @de.ne = @tvarOver30 .. @tvarOver60 is backwards and Collect! will not be able to calculate this. Put the earlier date first in your range.

Many reports in Collect! use variables. Review them for additional examples. Also, you may use the Help Index and look for "Variables."

Top of page.

See Also

- How To Use Variables
- How To Assign And Calculate Using Variables
- How To Format Variables When Assigning
- Report Sample to view sample reports and letters
- Report Topics Index for a list of all report and letter topics

Top of page.

Was this page helpful? Do you have any comments on this document? Can we make it better? If so how may we improve this page.

Please click this link to send us your comments: helpinfo@collect.org