So far, you have only been using
Debug.Print to
print simple text messages to yourself, but you can also use it to display data
values. (Because some of your text messages have used string variables, perhaps
that is obvious.) A few rules apply to data variables when they are displayed
via
Debug.Print. Most of these rules apply equally
well to every other aspect of VB, so they are pretty straightforward and should
not cause you any difficulty even if you don’t remember them.
The Immediate window is aware of any locale settings you have established for
your system. Consequently, if you use Debug.Print to
display numeric data, the values will be displayed with the appropriate decimal
separator, and any keywords will appear in your chosen language. Date variables
will be displayed in the short date format recognized by your system. Boolean
values are displayed as either true or false. These are the same default behaviors
you should see anywhere in VB.
If you are testing variables to see whether they contain values, remember that
empty is not the same state as null. If the value of an empty variable is displayed,
nothing is printed (not the word nothing, but literally nothing). If the variable
is null, the word Null is printed in the Immediate window.
Here are some examples of how data may be output to the Immediate window:
LISTING 18.4
OUTPUT OF DATA TO THE IMMEDIATE WINDOW
Sub DebugPrintExamples()
‘ Display a decimal value
Dim s As Single
s = 3.14159
Debug.Print "The value of s is"; s
‘ Display a Boolean
Dim fMakesSenseToMe As Boolean
Debug.Print "The default value of a Boolean is ";
fMakesSenseToMe
‘ Display a date
Dim d As Date
d = Date
Debug.Print "Today is "; d
‘ Empty vs. Null
Dim var1 As Variant, var2 As Variant
Debug.Print "Before any assignment, a Variant is "; var1
Debug.Print "Can’t see that? Of course not; it really IS
empty!"
Debug.Print "OK, True or False. The statement ‘var1 is
Empty’ is "; IsEmpty(var1)
Debug.Print "and the statement ‘var1 is Null’ is ";
IsNull(var1)
‘ After an assignment, can var be reassigned a value of
Empty?
var1 = 1: var1 = Null
Debug.Print "Now, var1 is "; var1;
Debug.Print ", so the value of IsEmpty(var1) is ";
IsEmpty(var1)
Debug.Print "Not surprisingly, the statement ‘var1 is Null’
is "; IsNull(var1)
var1 = Empty
Debug.Print "A Variant can become Empty again by
assignment: "; IsEmpty(var1)
var1 = 1: var1 = var2
Debug.Print "or by being assigned the value of another
empty Variant: "; IsEmpty(var1)
End Sub
It used to be that after a Variant had been assigned a value, it could become
empty again only by being assigned the value of another empty Variant. Now you
can just reassign the value empty to the variable.