Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2005, 03:09 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default Gracefully Handling Nulls

In MS Access VBA, there is a terrific function, Nv(). It takes two arguments. The first argument is tested for whether it is Null or not.
If it is [u]not</u> Null, the first argument is returned.
If it [u]is</u> Null, the second argument is returned.
So then, the following:
Code:
    Dim x As Variant
    Dim v As Variant

    x = Null
    v = Nz(x, "")    ' v now is an empty string
    v = Nz(x, 5)     ' v now is an integer, 5.

    x = 1
    v = Nz(x, "")    ' v now equals 1

    x = "Hey..."
    v = Nz(x, "")    ' v now equals "Hey..."

I liked this so much, I added that functionality, with the same exact syntax, to a VB6 library of functions that I included with my projects.
I would much prefer to find that .NET has covered this, than to once again write my own...

Can it be that MS [u]still</u> has not added this functionality to VB? Oracle has a function that will do the same thing in SQL (NVL()), Access has it—it seems that more than one company has found this to be a useful process...

I am presuming that ADO.NET still produces Nulls when the contents of the DB for a given field in a given row is carrying a null in the data.
 
Old May 19th, 2005, 07:58 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Brian,

You're absolutely right. This would be great to have in .NET, however I've yet to run across it. I have a helper function for dealing with this in my systems. I just spent some time browsing thru the Microsoft.VisualBasic namespace and still didn't find anything that did that.

-Peter
 
Old May 20th, 2005, 09:57 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I just this morning came acros the ability to decorate (if that's the right term) a function declaration to prevent seeing any of its code while single-stepping. If a routine has
Code:
    <System.Diagnostics.DebuggerStepThrough()> Private Sub . . .
in front of it, it will be skipped over, just as internal functions like SomeString.Replace() would be.

Armed with that, I can make my Nz() function, and not have to continually step through its already-tested code.

My VB6 code is
Code:
Public Function Nz(Value As Variant, Optional ValueIfNull As Variant) As Variant

    On Error GoTo Er

    If IsNull(Value) Then
            If IsMissing(ValueIfNull) Then Nz = 0 _
                                      Else Nz = ValueIfNull
    Else
        Nz = Value
    End If

    Exit Function

Er: Err.Raise Err.Number, "Function Nz()", Err.Description

End Function
 
Old May 25th, 2005, 02:06 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Here’s what I wound up with:
Code:
<System.Diagnostics.DebuggerStepThrough()> _
Public Function Nz(ByVal Value, Optional ByVal ValueIfNull = 0)

    If Value Is DBNull.Value Then Return ValueIfNull _
                             Else Return Value

End Function
I wish I didn’t [u]have</u> to specify a value for an Optional argument, but I do. (What are you gonna do?)





Similar Threads
Thread Thread Starter Forum Replies Last Post
IN and NULLs joxa83 SQL Server 2005 8 September 29th, 2008 03:03 AM
WHERE Clause, LIKE, and Nulls jurio SQL Language 5 May 28th, 2007 02:53 AM
Gracefully Handling Nulls BrianWren VB.NET 2002/2003 Basics 0 May 19th, 2005 04:10 PM
looking for nulls Warbird C# 4 July 1st, 2004 07:50 AM
Handling Nulls shahchi1 ADO.NET 4 June 24th, 2004 11:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.