473,791 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Emulate Nz Function

Anyone have code that emulates the Nz function in Microsoft Access?

In Access it is:

Nz(Value as variant, Optional ValueIfNull as Variant) as Variant

Nov 21 '05 #1
4 7698
well you could use the IIF function to simulate the behavior of Nz
regards

Michel Posseth

"Paul" <pw****@hotmail .com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Anyone have code that emulates the Nz function in Microsoft Access?

In Access it is:

Nz(Value as variant, Optional ValueIfNull as Variant) as Variant

Nov 21 '05 #2
Try this and see if it is what you are looking for. I took some liberties
as to what NZ does, as I do not have Access and the documenation online
(that I could find) wasn't too clear.

I find that IIF is cumbersome when evaluating multi-step operations such as
IIF(((Value is Nothing) orelse (isnumeric(Valu e) andalso
Value=0)),SomeN ewValue,Value)

Finally, this will only work at the code level, and you cannot use this
function in SQL like you could have in Access.

Dim b As Object
Dim s As String
Dim y As Decimal
Dim x As Date

Debug.WriteLine ("Fails NZ Test")
Debug.WriteLine (String.Format( "Object:-{0}-", AccessHelperFun ctions.NZ(b,
CType("", String)).ToStri ng))
Debug.WriteLine (String.Format( "String:-{0}-", AccessHelperFun ctions.NZ(s,
CType("", String)).ToStri ng))
Debug.WriteLine (String.Format( "Decimal:-{0}-", AccessHelperFun ctions.NZ(y,
CType("", String)).ToStri ng))
Debug.WriteLine (String.Format( "Date:-{0}-", AccessHelperFun ctions.NZ(x,
CType("", String)).ToStri ng))

Debug.WriteLine ("")
Debug.WriteLine ("")

Debug.WriteLine ("Passes NZ Test")
Debug.WriteLine (String.Format( "Object:-{0}-", AccessHelperFun ctions.NZ(New
DataSet, CType("", String)).ToStri ng))
Debug.WriteLine (String.Format( "String:-{0}-",
AccessHelperFun ctions.NZ("Some TextHere", CType("", String)).ToStri ng))
Debug.WriteLine (String.Format( "Decimal:-{0}-",
AccessHelperFun ctions.NZ(15.67 89, CType("", String)).ToStri ng))
Debug.WriteLine (String.Format( "Date:-{0}-", AccessHelperFun ctions.NZ(New
DateTime(12, 3, 4), CType("", String)).ToStri ng))

Debug.WriteLine ("")
Debug.WriteLine ("")

Debug.WriteLine (String.Format( "NZ Uses This Application:-{0}-",
AccessHelperFun ctions.NZUseCou nt.ToString))

Results:
Fails NZ Test
Object:--
String:--
Decimal:--
Date:--
Passes NZ Test
Object:-System.Data.Dat aSet-
String:-SomeTextHere-
Decimal:-15.6789-
Date:-3/4/0012 12:00:00 AM-
NZ Uses This Application:-8-

Code:
Public Class AccessHelperFun ctions

Private Shared m_NZUseCount As Decimal

Shared Sub New()
m_NZUseCount = 0
End Sub

#Region " NZ "

Public Shared Function NZ(ByVal Value As Object) As Object

Return (NZ(Value, True, Nothing))

End Function

Public Shared Function NZ(ByVal Value As Object, ByVal FailOnDefaultDa te
As Boolean) As Object

Return (NZ(Value, FailOnDefaultDa te, Nothing))

End Function

Public Shared Function NZ(ByVal Value As Object, ByVal ValueWhenNull As
Object) As Object

Return (NZ(Value, True, ValueWhenNull))

End Function

Public Shared Function NZ(ByVal Value As Object, ByVal FailOnDefaultDa te
As Boolean, ByVal ValueWhenNull As Object) As Object

'
' Tests if a given value is Null or Zero
' If true return the ValueWhenNull
' If false return the Value
'
' Optionally, test for default date values
'
'Usage counter variable, omit if you want
NZUseCountIncre ment()

'Check if the value is null or zero
If Value Is Nothing OrElse (IsNumeric(Valu e) AndAlso Value = 0) Then
'Return the ValueWhenNull
Return ValueWhenNull '"Nothing"

'Check if the value compares to System.DBNull, this should never happen
ElseIf Value Is System.DBNull.V alue Then
'Return the ValueWhenNull
Return ValueWhenNull '"System.DBNull "

'Check if this is a date, that we are to check for min date values, and
it is the min value
ElseIf FailOnDefaultDa te AndAlso (IsDate(Value) AndAlso Value =
Date.MinValue) Then
'Return the ValueWhenNull
Return ValueWhenNull '"System.Date.M inValue"

'Check if this is a string and if it is set to String.Empty, we should
never get to here
ElseIf (TypeOf Value Is String) AndAlso (CType(Value, String) Is
String.Empty) Then
'Return the ValueWhenNull
Return ValueWhenNull '"String.Emp ty"

Else
'Return the Value
Return Value
End If

End Function

Private Shared Sub NZUseCountIncre ment()
' Increments the number of times
' NZ was called
'
m_NZUseCount += 1
End Sub

Public Shared ReadOnly Property NZUseCount() As Decimal
' Returns the number of times NZ was called
' this value is over the application lifetime
' as shared objects are not unloaded once loaded
Get
Return m_NZUseCount
End Get
'
End Property

#End Region

End Class
"Paul" <pw****@hotmail .com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Anyone have code that emulates the Nz function in Microsoft Access?

In Access it is:

Nz(Value as variant, Optional ValueIfNull as Variant) as Variant

Nov 21 '05 #3
That's very close. The only thing is that zero is valid.

Nov 21 '05 #4
Thanks. That's good code. I wasn't sure how to handle the date issue.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1609
by: bulk88 | last post by:
How can I emulate DOM level 1 removeChild in IE 4? I already figured out to emulate getElementById by doing this if((!document.getElementById) && document.all) {document.getElementById = function(id){return document.all;}; } Is there something similar for removeChild?
5
55167
by: deko | last post by:
Is there a way to programmatically emulate pressing the Escape key with VBA? (Access 2003) What I'm trying to do is clear a field in a form (which otherwise requires the user to press Escape) after trapping a particular error. For example, if the user enters a value in a form field that meets certain criteria, I want to trap this as an error and clear the field with code (same thing as pressing the Escape key). What code should I use...
2
1417
by: bjbounce2002 | last post by:
Hello, Can Access 97 emulate Windows Find Command and specifically the 'Containing Text' function? What we want is for Access to search within and find Word documents on our networked drives and provide a list of search results - without having to link all documents of the destination folder/s? If this is possible, would anyone have any code available or is anyone
61
16302
by: /* frank */ | last post by:
I have to do a homework: make a CPU simulator using C language. I have a set of asm instructions so I have to write a program that should: - load .asm file - view .asm file - do a step by step simulation - display registers contents I tried to search con google with no success.
7
1136
by: Roy Smith | last post by:
I think I know the answer to this, but I'll ask it just in case there's something I hadn't considered... I'm working on a python interface to a OODB. Communication with the DB is over a TCP connection, using a model vaguely based on CORBA. I'll be creating object handles in Python which are proxies for the real objects in the database by doing something like: handle = connection.getObjectHandle (className, instanceName)
11
2086
by: TempestV | last post by:
Hi All, I have a front end / back end MS Access DB at my workplace. When linking the front-to-back ends in the Linked Table Manager, I specify the network location of the backend as \\servername\sharename\backendname.mdb, which works fine. However, I want to do some development at home but I don't want to have to keep re-linking the tables (this is time consuming as there's actually more than one backend .mdb file). Is there a way I...
3
2136
by: mario.rossi | last post by:
Hello! I would like to write a pointer to type template that behaves exactly and completely like normal pointers, which I will use as starting point for other kinds of pointers that hold extra data (for example, one of my aims is to have 64bit pointers on a 32bit machine, and ignore the highmost 32bits of the address, while still keeping it in structures and function parameters, so I can start to migrate my 32bit OS to 64bit, although I...
8
1679
by: JimS | last post by:
In my environment we are using DB2 UDB for Windows for development and DB2 UDB for z/OS for production. In DB2 UDB for z/OS uniqueness of columns that permit nulls may be enforced with a unique index created with the "WHERE NOT NULL" parameter; this has the effect of ensuring that not null columns are unique while not considering null columns to be duplicates. Does anyone know of a way to duplicate this behavior with DB2 UDB for Windows?
8
2463
by: Giovanni R. | last post by:
Take a look at this code (you can execute it): error_reporting(E_ALL); function byVal( $v) {} function byRef(&$v) {} print '<pre>'; byVal ($first); // gives a notice
14
1724
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.