[dba-VB] Checking for NULL and empty strings

David McAfee davidmcafee at gmail.com
Thu May 24 19:41:29 CDT 2007


Here's a function I wrote (actually I wrote it for C#, but crossed it over
to vb.net)

    Public Function IsNull(ByVal strInput As String, ByVal strReplace As
String) As String
        'This works like SQL server's IsNull, not VB/VBA. In VB/VBA it is
known as Nz()
        If (strInput Is Nothing) Then
            Return strReplace
        Else
            If strInput = "" Then
                Return strReplace
            Else
                Return strInput
            End If
        End If
    End Function


On 5/24/07, David Emerson <newsgrps at dalyn.co.nz> wrote:
>
> Group,
>
> I am wanting to test the value of a field from a data source.  If it
> is NULL or "" then do one thing, otherwise do another.  Here is my vb.netcode:
>
>          If IsDBNull(Fields("MAddress2").Value) Or
> (Fields("MAddress2").Value = "") Then
>              Me.txtMailAddress.DataField = "=MAddress1"
>          Else
>              Me.txtMailAddress.DataField = "=MAddress1" + Chr(34) +
> ", " + Chr(34) + " + MAddress2"
>          End If
>
> The error message is "
>
> Operator '=' is not defined for type 'DBNull' and string "".
>
> What is the correct syntax for this?
>
>
> Regards
>
> David Emerson
> Dalyn Software Ltd
> Wellington, New Zealand
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
>
>



More information about the dba-VB mailing list