The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

# The Boolean Data Type

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The Boolean data type is a special case of an integer data type. The Boolean data type can contain True or False; internally, VBA stores the value of True as -1, and the value of False as 0.

You can use the CBool function to convert any numeric value to a Boolean value. When another numeric data type is converted to a Boolean value, any nonzero value is equivalent to True, and zero (0) is equivalent to False. For example, `CBool(7)` returns True, and `CBool(5 + 2 – 7)` returns False, since it evaluates to `CBool(0)`.

The following procedure determines whether a number is even. The procedure uses the Mod operator to determine whether a number can be divided by 2 with no remainder. If a number is even, dividing by 2 leaves no remainder; if it's odd, dividing by 2 leaves a remainder of 1:

``````Function IsEven(lngNum As Long) As Boolean
' Determines whether a number is even or odd.

If lngNum Mod 2 = 0 Then
IsEven = True
Else
IsEven = False
End If
End Function
``````

Note   For more information about the Mod operator, see "Using the Mod Operator" later in this chapter, or search the Visual Basic Reference Help index for "Mod operator."

Another way to write this procedure is to convert the result of an expression to a Boolean value and then use the Not keyword to toggle its value, as shown in the following example. If the lngNum argument is odd, then it must be nonzero; converting lngNum to a Boolean value yields True. Since the procedure must return False if the value is odd, using the Not keyword to toggle the Boolean value gives the correct result.

``````Function IsEven(lngNum As Long) As Boolean
' Determines whether a number is even or odd.

IsEven = Not CBool(lngNum Mod 2)
End Function
``````

Note that the revised IsEven procedure condenses a five-line If…Then statement into a single line of code. If you're using an If…Then statement to set a value to True under one condition and to False under another, as the IsEven procedure does, you can condense the If…Then statement by modifying its condition to return True or False. However, the revised procedure may be somewhat harder to understand. The revised IsEven procedure is available in the modNumbers module in VBA.mdb in the ODETools\V9\Samples\ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM.

Show: