IsEmpty - VBScript
Overview
IsEmpty is a VB Script command used to evaluate whether a variable or object has a value or not. It returns a Boolean value, True if the variable or object is empty and False if it has a value.
Syntax
IsEmpty(expression)
Parameters:
- expression: The variable or object to evaluate. Can be a string, array, function, object, or any other VBA data type.
Options/Flags
None
Examples
- Check if a string variable is empty:
Dim myString As String
myString = ""
If IsEmpty(myString) = True Then
MsgBox "The string is empty."
End If
- Check if an array is empty:
Dim myArray() As Integer
ReDim Preserve myArray(0)
If IsEmpty(myArray) = True Then
MsgBox "The array is empty."
End If
- Check if an object is empty:
Dim myObject As Object
Set myObject = Nothing
If IsEmpty(myObject) = True Then
MsgBox "The object is empty."
End If
Common Issues
- Confusing IsEmpty with IsNull: IsEmpty checks if a variable or object has a value, while IsNull checks if a variable or object is specifically assigned to Null.
- Not using parentheses: The IsEmpty function requires parentheses around its expression. Failure to do so will result in a syntax error.
Integration
IsEmpty can be combined with other VB Script commands to perform various tasks:
- Check if a form field is empty and display an error message:
If IsEmpty(Me.txtName) = True Then
MsgBox "Please enter your name."
End If
- Assign a default value to an empty variable:
Dim myVariable As String
If IsEmpty(myVariable) Then
myVariable = "Default Value"
End If
Related Commands
- IsNull: Checks if a variable or object is specifically assigned to Null.
- IsNullOrEmpty: Checks if a variable or object is empty or Null.