473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7662
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*********************@f14g2000cwb.googlegro ups.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(Value) andalso
Value=0)),SomeNewValue,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}-", AccessHelperFunctions.NZ(b,
CType("", String)).ToString))
Debug.WriteLine(String.Format("String:-{0}-", AccessHelperFunctions.NZ(s,
CType("", String)).ToString))
Debug.WriteLine(String.Format("Decimal:-{0}-", AccessHelperFunctions.NZ(y,
CType("", String)).ToString))
Debug.WriteLine(String.Format("Date:-{0}-", AccessHelperFunctions.NZ(x,
CType("", String)).ToString))

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

Debug.WriteLine("Passes NZ Test")
Debug.WriteLine(String.Format("Object:-{0}-", AccessHelperFunctions.NZ(New
DataSet, CType("", String)).ToString))
Debug.WriteLine(String.Format("String:-{0}-",
AccessHelperFunctions.NZ("SomeTextHere", CType("", String)).ToString))
Debug.WriteLine(String.Format("Decimal:-{0}-",
AccessHelperFunctions.NZ(15.6789, CType("", String)).ToString))
Debug.WriteLine(String.Format("Date:-{0}-", AccessHelperFunctions.NZ(New
DateTime(12, 3, 4), CType("", String)).ToString))

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

Debug.WriteLine(String.Format("NZ Uses This Application:-{0}-",
AccessHelperFunctions.NZUseCount.ToString))

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

Code:
Public Class AccessHelperFunctions

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 FailOnDefaultDate
As Boolean) As Object

Return (NZ(Value, FailOnDefaultDate, 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 FailOnDefaultDate
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
NZUseCountIncrement()

'Check if the value is null or zero
If Value Is Nothing OrElse (IsNumeric(Value) 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.Value 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 FailOnDefaultDate AndAlso (IsDate(Value) AndAlso Value =
Date.MinValue) Then
'Return the ValueWhenNull
Return ValueWhenNull '"System.Date.MinValue"

'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.Empty"

Else
'Return the Value
Return Value
End If

End Function

Private Shared Sub NZUseCountIncrement()
' 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*********************@f14g2000cwb.googlegro ups.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
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 =...
5
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)...
2
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...
61
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...
7
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...
11
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...
3
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...
8
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...
8
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
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.