Using the Default Attribute and Current Field Value
The value assigned to a property in design mode is called the property’s default attribute. When assigning a formula to that property, the default value is overridden by what is in the formula. There are many times when a formula is only used to specify what happens in a unique circumstance (e.g. an inventory item being out of stock) and you don’t want the formula to override the default value every time. The rest of the time you want the default value to be left unchanged. There are two ways of doing this: not specifying a value in the formula or using the keyword DefaultAttribute.
When you don’t specify a value, you are letting Crystal Reports use what was specified in the Format Editor. For example, you may want a field to be red if its value is less than the minimum quantity, otherwise use the attribute specified in the Format Editor. You can use a formula like the following:
If {Inventory.OnHand} < {InventoryItems.MinimumQty} Then
Formula = crRed
End If
If the condition is true, then the color becomes red. If it is false, then the color is left unchanged.
Although this is acceptable, it isn't perfectly clear what the color will be if the condition isn't true. As a second alternative you can use an Else statement and specify the result to be DefaultAttribute. By doing this you are telling someone reading your code that the color will either be crRed or the attribute that is specified on the Format Editor. The new code would look like the following:
If {Inventory.OnHand} < {InventoryItems.MinimumQty} Then
Formula = crRed
Else
Formula = DefaultAttribute
End If
Formulas can be made generic so that they can be used on different fields. By replacing the field name with the keyword CurrentFieldValue, the formula can be called in various places on the report. This keyword returns the current value of the field that is being formatted. The CurrentFieldValue is seen in Chapter 11 which discusses Cross-Tab reports.