通常,當 Crystal Reports 碰到公式中的空值欄位時,會立刻停止驗算公式,而且不產生任何值。如果您想要在公式中處理空欄位值,必須明確地使用專門設計來控制它們的函式來處理:IsNull、PreviousIsNull 或 NextIsNull。
與運算子相關,在 Crystal Reports 驗算下列條件時:
IsNull({Product.Color}) Or _ InStr({Product.Color}, " ") = 0
程式會先驗算 IsNull ({Product.Color}),在決定結果為 True 之後,程式就會知道整個條件均為 True,而不需檢查是否
InStr({Product.Color}, " ") = 0
換句話說,Crystal Reports 在預測出整個運算式的結果後,就會停止驗算布林運算式。在下列的範例中,在 denom 為 0 的情況下,公式不會以零來當除數:
Dim num As Number, denom As Number ... If denom <> 0 And num / denom > 5 Then ...
注意 Visual Basic 不支援這項技巧,因為 Visual Basic 中布林運算式的所有部分都會被驗算,就算沒有必要也是如此。
範例
{Product.Color} 欄位包含「紅」及「黑」等基本色彩,和較具描述性的複合字色彩,例如「絲綢銀」和「寶石綠」。以下是公式範例,此公式將基本色彩標為「基本」,而將其他色彩標為「花式」。
If InStr({Product.Color}, " ") = 0 Then formula = "basic" Else formula = "fancy" End If
函式呼叫 InStr 會去搜尋 {Product.Color} 字串中的空格。假如它找到空格,就會傳回空格的位置;否則傳回 0。由於基本色彩都只有一個英文字,不含任何空格,因此 InStr 碰到它們就會傳回 0。
對於 Guardian Chain Lock 這類產品,並不會記錄它的色彩值,所以在資料庫中該筆資料錄的 {Product.Color} 欄位具有空值。因此,Guardian Chain Lock 資料錄的旁邊不會印出任何文字。
以下範例說明如何使用 IsNull 來修復上述的範例:
If IsNull({Product.Color}) Or _ InStr({Product.Color}, " ") = 0 Then formula = "basic" Else formula = "fancy" End If