by Jared Scarbrough (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (11 Votes)
This code provides an IsNull function like that used in SQL Server. This allows you to check if a variable contains null, and if so return a specific value. This can prevent you from having to write things like iif(not isnull(rst!FieldName), rst!FieldName, ""). For new programmers, especially those working with databases, you will quickly become aware that trying to access a database field that contains a null value often results in the dreaded error #94 - Invalid Use of Null, which means you constantly have to write code to trap for that possibility.
Inputs
ValueToCheck = a variant representing he value to check for null
varWhatToReturnIfNull = the value to return if ValueToCheck is null
Code Returns
If ValueToCheck is null, then varWhatToReturnIfNull is returned, otherwise, ValueToCheck is returned
Public Function IsNullEx(ValueToCheck As Variant, varWhatToReturnIfNull) As Variant
If IsNull(ValueToCheck) Then
IsNullEx = varWhatToReturnIfNull
Else
IsNullEx = ValueToCheck
End If
End Function
Usage example:
txtClientName = IsNullEx(rst!ClientName, "unknown")