MS Access: Nz Function - TechOnTheNet

totn Access Functions MS Access: Nz Function

This MSAccess tutorial explains how to use the Access Nz function with syntax and examples.

Description

The Microsoft Access Nz function lets you return a value when a variant is null.

Syntax

The syntax for the Nz function in MS Access is:

Nz ( variant, [ value_if_null ] )

Parameters or Arguments

variant A variable that is a variant datatype. value_if_null Optional. It is the value to use when the variant is a null value. If this parameter is omitted and the variant is a null value, the Nz function will return a zero or a zero-length string.

Returns

The Nz function returns the variant if the value of variant is not null. The Nz function returns the value_if_null if variant has a null value.

Applies To

The Nz function can be used in the following versions of Microsoft Access:

  • Access 2019, Access 2016, Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the Nz function in MS Access:

Nz (varName, "n/a")

The example above would return the value 'n/a' if the variable varName contained a null value.

Nz (varName)

The example above would return a zero-length string if the variable varName contained a null value.

Example in VBA Code

The Nz function can be used in VBA code in Microsoft Access.

For example:

Dim LOption As String LOption = Nz(varChoice, "Not Found")

In this example, the variable called LOption would now contain value in the varChoice variable unless it was a null value. If the varChoice variable contains a null value, the Nz function will return "Not Found".

Example in SQL/Queries

You can also use the Nz function in a query in Microsoft Access.

For example:

Microsoft Access

In this query, we have used the Nz function as follows:

Expr1: Nz([CategoryName],'Not Found')

This query will evaluate whether the CategoryName field contains a null value and display the results in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.

So in this example, if the CategoryName field contains a null value, the Nz function will return "Not Found". Otherwise, it will return the value from the CategoryName field.

For example:

Category: Nz([CategoryName],'Not Found')

The results would now be displayed in a column called Category.

Từ khóa » Hàm Nz Trong Access