VB3 PRB: Error If Assign DB Value to Var: Invalid Use of NullLast reviewed: January 9, 1997Article ID: Q113032 |
The information in this article applies to:
SYMPTOMSIt is possible to receive the error message "Invalid Use of Null" when assigning data from an empty field in a database to a variable or control. This article explains why this error occurs and how to work around it.
CAUSEThe variant data type can hold several types of data. It can also be Null or Empty. It is important to distinguish between Null and Empty. A Null variant contains no valid data, while an Empty variant has not been initialized. When an Empty variant is assigned to a string it is converted to "". When an Empty variant is assigned to a numeric it is converted to 0. The Null variant on the other hand has no valid data, so it cannot be assigned to a string or a numeric. Trying to assign a Null variant generates the "Invalid Use of Null" error. The following example demonstrates this behavior.
Dim a as Variant Dim b as Integer a = Null b = aSome properties, functions, and methods also return Null. An obvious example is the Null function. To avoid the "Invalid Use of Null" error, don't assign a function or method that returns Null to a string or numeric. The following example demonstrates this behavior:
Dim b as Integer b = NullThe "Invalid Use of Null" error can also occur when assigning a value to a string or numeric property of a control. The text property of a text box is a string property. The following example shows how the "Invalid Use of Null" error can occur with a text box.
Text1.Text = NullIn database programming, you may also receive the "Invalid Use of Null" error when assigning the value of a field to a text box. This happens because the Value property returns Null when the field contains no valid data. Here's an example that demonstrates this:
WORKAROUNDVisual Basic provides two mechanisms for working around the error.
The IsNull() Function MethodThe IsNull() function allows you to detect Null. Here's how you could use IsNull() in a database program:
The Ampersand (&) Concatenation MethodThe other method is to take advantage of Visual Basic's string concatenation operator -- the ampersand (&). If one of the arguments in a concatenation is valid and the other is Null, a concatenation will convert the null value to "". You can take advantage of this behavior when assigning values that might return Null. When concatenating a valid string with a value that could return null, the result will always be a valid string. Here's an example that uses string concatenation:
|
KBCategory: kbprg kbcode kbprb
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |