Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA NZ() Replacement in .NET

Status
Not open for further replies.

ZmrAbdulla

Technical User
Apr 22, 2003
4,364
AE
Is there any replacement for VBA Nz() function in VB.NET?
I can't find any..
Thanks

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
I'm afraidd it doesn't exist, but have a look at this


its the reply by AMDRIT that you are looking for.

It seems to cover most eventualities, and with the source code as a template should be relatively easy to add additional datatypes.


Hope this helps.

[vampire][bat]
 
I use a very simple function that exactly mimics the old NZ:

Code:
Public Shared Function Nz(ByVal Value As Object, Optional ByVal Default as Object = String.Empty) As Object
    If Value Is Nothing OrElse IsDBNull(Value) Then
        Return Default
    Else
        Return Value
    End If
End Function

You will have to take care of any necessary type casting yourself but it has the merit of being extremely simple.

Note that you can't use the IIf function because if the item is nothing or null it will give an exception. This is because the code evaluates the entire command before executing the appropriate true/false part.



Bob Boffin
 
Thanks to you both..
I can't understand why did MS swallow the NZ function.
I found a ]similar one in MSDNYou may need to copy & paste the entire link to brwoser.

I think IIF() will be better than a long lines of code but I fail to get it work IIF() too.
what I want to do is if the item is null then should not throw an error.
Code:
Me.TextBox2.Text = ds1.Tables(sql1).Rows(0).Item(2)

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
You could use IsDBNull in that scenario e.g.
Code:
If Not IsDBNull(ds1.Tables(sql1).Rows(0).Item(2)) Then Me.TextBox2.Text = ds1.Tables(sql1).Rows(0).Item(2)


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
There never was an Nz function in VB6; it was only in VBA so that's why they didn't carry it through. VB.NET is replacement for VB6 not VBA.


Bob Boffin
 
As far as I'm aware, NZ is not a "true" VBA function. I think it was only available in Access. It probably predates VBA (i.e Access Basic) and was kept in because of its usefulness.

[vampire][bat]
 
ca8msm,
that wroks great. only thing i need to clear the textboxes before it bring details of the newly selected record as it is in VB6.

earthandfire & Bob,
Yes it should be Access VBA function. I don't remember I worked in VB6 with NZ().


________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top