Practical Web Programming

Thursday, November 15, 2007

How To Handle Null Values in Database in VB6

Null value is just so frustrating. If you don't anticipate and handle it, it will create unpredictable results in your program. With null values unhandled, runtime error are very likely to occur.

If you are a business application programmer, you need to take null values seriously, especially when accessing and referencing data from a database. In my experience as a programmer, this is one of most causes of runtime error in programs written by newbie (even experienced) programmers.

This function help you in handling null values from a database. It will checks a value for null and returns a non-null value or a default value.

'Language: Visual Basic 6 (VB6)
'Parameter: varValue = the value that needs to be handle
' varNullValue = the optional value to return in
' case the varValue is Null
'Usage: MsgBox IfNull(rst!name, "kabalweg")

Public Function IfNull(varValue As Variant, Optional varNullValue As Variant = "") As Variant
If IsNull(varValue) Or varValue = "" Then
IfNull = varNullValue
IfNull = varValue
End If
End Function


Recent Post