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. CAUSE
The 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.
Some 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:
The "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.
In 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:
WORKAROUND
Visual 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:
Additional query words: 3.00 Access
Keywords : kbcode PrgOther |
Last Reviewed: September 17, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |