Your code has to be concerned with receiving Null values from a database. The reson I say that you need to be concerned is that, since Null is an indicator of invalid data, Null can cause trouble for you if you pass it to certain functions or try and use it to perform mathematical operations. Try the Code given in next post.
First
Next
Null Example
'This code does not work like you might expect
Dim varTest
varTest = Null
If varTest = Null Then
MsgBox "The variable has a Null value."
End If
You did not see any dialogue box pop up did you? That is because the expression If varTest = Null always returns False. If you want to know if a a variable contains a Null value, you must use the IsNull ( ) function:
If IsNull(varTest) = True Then
MsgBox "The variable has a Null value."
End If
First
Next
Dim varTest
varTest = Null
If varTest = Null Then
MsgBox "The variable has a Null value."
End If
You did not see any dialogue box pop up did you? That is because the expression If varTest = Null always returns False. If you want to know if a a variable contains a Null value, you must use the IsNull ( ) function:
If IsNull(varTest) = True Then
MsgBox "The variable has a Null value."
End If
First
Next
Testing Null
The syntx for assigning and testing for Null values is similar to the way the Empty value/subtype works. Here is some code that assigns a Null value to a variable:
varTest = Null
However, you cannot directly test for the value of Null in the same way that you can with Empty - you must use only the IsNull ( ) function to test for a Null value. This is because Null represents invalid data, and when you try to make a direct comparison using invalid data, the result is always invalid data. Try running the code given on next post.
First
Next
varTest = Null
However, you cannot directly test for the value of Null in the same way that you can with Empty - you must use only the IsNull ( ) function to test for a Null value. This is because Null represents invalid data, and when you try to make a direct comparison using invalid data, the result is always invalid data. Try running the code given on next post.
First
Next
Null From Another Angle
Another way to think about it is that Empty generally happens by default - it is implicit, because a variable is Empty until you place something in it. Null, on the other hand, is explicit - a variable can only be Null, if some code made it that way.
First
Next
First
Next
Null
The value/subtype of Null, in a confusing way, is similar to the value/subtype of Empty. The distinction may seem esoteric, but Empty indicates that a variable is uninitialized, whereas Null indicates the absence of valid data. Empty means that no value has been placed into a variable, whereas a Variant variable can only have the value/subtype of null after the value of Null has been placed into it. In other words, a variable can only be Null if the Null value has explicitly been placed into it. Null is a special value that is most often encountered in database tables. A column in a database is Null when there is no data in it, and if your code is going to read data from a database, you have to ready for Null values.
First
Next
First
Next
Test Empty
You can also test for whether a variable is empty in either of two ways:
If varTest = Empty Then
MsgBox "The variable is empty."
End If
OR:
If IsEmpty(varTest) Then
MsgBox "The variable is empty."
End If
The IsEmpty ( ) function returns a Variant value of the Boolean subtype with the value of True if the variable is empty, False if not.
First
If varTest = Empty Then
MsgBox "The variable is empty."
End If
OR:
If IsEmpty(varTest) Then
MsgBox "The variable is empty."
End If
The IsEmpty ( ) function returns a Variant value of the Boolean subtype with the value of True if the variable is empty, False if not.
First
Empty Example 2
Dim varTest
MsgBox CLng(varTest)
MsgBox CStr(varTest)
When you run this script, it will produce two dialogue boxes. The first box displays a 0 because Empty is 0 when represents as a number. The second box displays nothing because Empty is an "empty" or "zero length" string when represented as a String.
Once you place a value in a variant variable, it is no longer empty. It will take on another subtype, depending on what type of value you place in it. This is also true when you use a conversion function to coerce the subtype. However, if you need to, you can force the variable to become empty again by using the Empty keyword directly:
varTest = Empty
First
Next
MsgBox CLng(varTest)
MsgBox CStr(varTest)
When you run this script, it will produce two dialogue boxes. The first box displays a 0 because Empty is 0 when represents as a number. The second box displays nothing because Empty is an "empty" or "zero length" string when represented as a String.
Once you place a value in a variant variable, it is no longer empty. It will take on another subtype, depending on what type of value you place in it. This is also true when you use a conversion function to coerce the subtype. However, if you need to, you can force the variable to become empty again by using the Empty keyword directly:
varTest = Empty
First
Next
Empty Example 1
Dim varTest
MsgBox TypeName(varType)
Run the script using Windows Script Host and you'll see the value as "Empty". The subtype is Empty because we have not yet placed any value in it. Empty is both the initial subtype and the initial value of the variable. However, Empty is not a value that you can really do nothing with. You can display it on the screen or print it on paper. It only exists to represent the condition of the variable not having had any value placed in it. Try the code on next page.
First
Next
MsgBox TypeName(varType)
Run the script using Windows Script Host and you'll see the value as "Empty". The subtype is Empty because we have not yet placed any value in it. Empty is both the initial subtype and the initial value of the variable. However, Empty is not a value that you can really do nothing with. You can display it on the screen or print it on paper. It only exists to represent the condition of the variable not having had any value placed in it. Try the code on next page.
First
Next
Empty
Empty is a special value that can only be held in a Variant variable. In Visual Basic, variables declared as any of the specific data types cannot hold the value of Empty-only variables declared as Variant. In VBScript, of course, all variables are Variant variables. A Variant variable is "empty", and has the Empty subtype, after it has been declared, but before any value has been placed within it. In other words, Empty is the equivalent of "not initialized". Once any type of value has been placed into the variable, it will take on one of the other subtype, depending on what the value is. Let us take a look at some examples:
First
Next
First
Next
Empty and Null
Empty and Null are special in that they do not have a corresponding specific Visual Basic data type. In fact, it is a bit of misnomer to call these subtypes, because they are actually special values that a Variant variable can hold. When the subtype of a variable is Empty or Null, its value is also either Empty or Null. This is different then the other subtypes, which only describe the type of value that the variable holds, not the value itself. For example, when the subtype of a variable is Long, the value of the variable can be 0, or 15, or 2,876,456, or one of about 4.3 billion numbers (-2,147,483,648 to 2,147,483,647). However, when the subtype of a variable is Empty, it's value is also always a special value called Empty. In the same fashion, when the subtype of a variable is Null, the value is always a special value called Null.
Empty
Null
Empty
Null
Subscribe to:
Posts (Atom)