Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero (0) length string, an empty Field, or no value at all - so exactly what is Null? The purpose of this Topic is hopefully to explain what a Null Value is, discuss some peculiarities about Nulls, show how we can detect them, and finally, how to convert them into not-so-devious entities.
- What is a Null Value?
- Null is a Variant subtype used to indicate that a data item contains no valid data, is missing data, or the data is simple unknown. This special value is not a real value like "Smith", #12/24/2006#, 25.98, etc. but is an indicator that data is missing, unknown, or not applicable. The data in a Table or Query Field, as well as in a Form or Report Control has the Variant Data Type by default. When you leave a Field or Control blank, Null is stored automatically. One critical point to remember, and one worth restating, is that only the Variant Data Type con contain Nulls.
- Some peculiarities concerning Null Values.
- If 'any' part of an Expressions evaluates to the Null Value, the entire Expression also has the Null Value. This is called propagation of Null Values and is a critical reason why we should take steps to handle them.
- If the Argument of a built-in or custom Function evaluates to Null, the Function usually returns a Null Value. For example, if you use SQL or an Aggregate Function to calculate a summary value of a Field for a group of Records, Records with Null Values in the Field won't be counted.
- When you join Tables in a Query, Records with Null Values in the join Field are not included in the Query result.
- When you create a Relationship and enforce Referential Integrity, you can still create Orphans in the Child Table by leaving the join Field blank in the Child Table.
- How can you detect Null Values?
- Fortunately, we have a Function that returns a Boolean value indicating whether or not an expression contains valid data (Null). This Function is the IsNull() Function, and consists of only 1 required Argument:
[CODE=vb]IsNull(<express ion>) 'returns True if expression is Null
'The required expression argument is a Variant containing a numeric expression or string expression.
[/CODE] - Converting Nulls to something less harmful.
- Fortunately, we have an ally in the never ending fight against Nulls, and this is the Nz() Function. You can use this Function to return zero, a zero-length string (" "), or another specified value when a Variant is Null. For example, you can use this function to convert a Null value to another value and prevent it from propagating through an expression.
[CODE=vb]Nz(variant, [valueifnull])[/CODE]
The variant Argument is a variable of data type Variant. The valueifnull Argument is Optional (unless used in a query). It is a Variant that supplies a value to be returned if the variant argument is Null. This argument enables you to return a value other than zero or a zero-length string.
If the value of the variant argument is Null, the Nz function returns the number zero or a zero-length string (always returns a zero-length string when used in a query expression), depending on whether the context indicates the value should be a number or a string. If the optional valueifnull argument is included, then the Nz function will return the value specified by that argument if the variant argument is Null. When used in a query expression, the NZ function should always include the valueifnull argument,
If the value of variant isn't Null, then the Nz function returns the value of variant.
[CODE=vb]Dim varVariant As Variant
varVariant = Null
Debug.Print Nz(varVariant) 'returns a Zero length String
Debug.Print Nz(varVariant, "Null") 'returns the String Null
Dim intTest As Integer
intTest = 600
Debug.Print Nz((intTest * varVariant) / 16, 9999) 'returns 9999[/CODE] - Fortunately, we have an ally in the never ending fight against Nulls, and this is the Nz() Function. You can use this Function to return zero, a zero-length string (" "), or another specified value when a Variant is Null. For example, you can use this function to convert a Null value to another value and prevent it from propagating through an expression.
Comment