Source code for Issue Number 10

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module (this is the desired way) or form. To do this, open up your project and insert a new Module. Change the name of the module to basNulls and paste this code into the module.

'----------------------------------------------------------------------
'
'   Module Name:    basNulls
'   Written By:     C&D Programming Corp.
'   Create Date:    10/30/97
'   Copyright:      Copyright 1997 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'   Purpose:        Several routines to make handling Null values
'                   easier.
'
'
'----------------------------------------------------------------------

Option Explicit

Function NullConvert(vDest As Variant, v As Variant) As Variant

    On Error GoTo ErrHandler

    Select Case TypeName(vDest)

        Case "String"
            vDest = NullConvertString(v)

        Case "Byte", "Long", "Integer", "Single", "Double", "Currency"
            vDest = NullConvertNumber(v)

        Case "Boolean"
            vDest = NullConvertBoolean(v)

        Case "Date"
            vDest = NullConvertDate(v)

        Case Else
            vDest = ""
    End Select

    Exit Function

ErrHandler:
    Err.Raise Err.Number
End Function

Function NullConvertDate(v As Variant) As Date
    If IsNull(v) Then
        NullConvertDate = Now   ' not sure this is the best default
    Else
        NullConvertDate = v
    End If
End Function

Function NullConvertCurrency(v As Variant) As Currency
    NullConvertCurrency = NullConvertNumber(v)
End Function

Function NullConvertLong(v As Variant) As Long
    NullConvertLong = NullConvertNumber(v)
End Function

Function NullConvertNumber(v As Variant) As Variant
    If IsNull(v) Then
        NullConvertNumber = 0
    Else
        NullConvertNumber = v
    End If
End Function

Function NullConvertString(v As Variant) As String
    If IsNull(v) Then
        NullConvertString = ""
    Else
        NullConvertString = v
    End If
End Function

Function NullConvertBoolean(v As Variant) As Boolean
    If IsNull(v) Then
        NullConvertBoolean = False
    Else
        NullConvertBoolean = v
    End If
End Function