How-to: Test for Empty or NULL or Zero [IsBlank function]

The IsBlank function below will return True if the variable or value passed to it is Empty or NULL or Zero.
It will return False if the variable contains any string or a value other than '0'.

Function IsBlank(Value)
'returns True if Empty or NULL or Zero
If IsEmpty(Value) or IsNull(Value) Then
 IsBlank = True
ElseIf VarType(Value) = vbString Then
 If Value = "" Then
  IsBlank = True
 End If
ElseIf IsObject(Value) Then
 If Value Is Nothing Then
  IsBlank = True
 End If
ElseIf IsNumeric(Value) Then
 If Value = 0 Then
  wscript.echo " Zero value found"
  IsBlank = True
 End If
Else
 IsBlank = False
End If
End Function

Arguably the numeric value '0' is as valid a value as any other number. The logic behind flagging this as blank is that you may have data like a Price or a Surname = 0 which most likely should be treated as being blank.

All vbscript variables are variants. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used.

Examples using the function above

Wscript.echo "testing 0..."
boolReturn = IsBlank(0)
if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

Wscript.echo "testing 123..."
boolReturn = IsBlank(123)
if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

Wscript.echo "testing 100-100..."
boolReturn = IsBlank(100-100)
if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

Wscript.echo "testing null..."
boolReturn = IsBlank(null)
if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

Wscript.echo "testing empty string..."
boolReturn = IsBlank("")
if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

Wscript.echo "testing string..."
boolReturn = IsBlank("The quick brown fox")
if boolReturn = true then wscript.echo "It's Blank" else wscript.echo "not blank"

“Let him that would move the world, first move himself” - Socrates

Related VBScript commands

IsNull - Is expression NULL?
IsNumeric - Is expression a Numeric?
IsEmpty - Is expression initialised?
Q250344 - Microsoft sample utility for array conversions.


 
Copyright © 1999-2024 SS64.com
Some rights reserved