|
|
|
You're probably aware that most VB functions don't work well with Null values, which is an issue when you're working with database columns that can accept Nulls. For example, the following statement: |
can raise error 94 "Invalid use of Null". The usual workaround is to force the conversion to string by appending an empty string, as in: |
However, this solution slightly affect code readability, especially if other people in your team aren't aware of the trick. A better and more flexible solution is to build a function that can convert a Null value to any other value: |
Click here to copy the following block | Function IfNull(value As Variant, Optional NullValue As Variant = "") As Variant If IsNull(value) Then IfNull = NullValue Else IfNull = value End If End Function |
You can use the above function with just one argument to replace the trick based on the empty string, but you can even display a more meaningful string (in reports, for example): |
Another handy function for Null handling mimicks the NullIf function in T-SQL, and is especially useful if you are importing data from a database or a text file that uses a special string - for example "Unknown" - in place of the Null constant, and you need to process these special strings as Null values: |
For example, you can easily convert all "Unknown" string to Null as follows: |
Note that you can use the NullIf function with string, numeric and date values. |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|