dbTalk Databases Forums  

nz for blanks in queries - formatting

comp.databases.ms-access comp.databases.ms-access


Discuss nz for blanks in queries - formatting in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
franc sutherland
 
Posts: n/a

Default nz for blanks in queries - formatting - 03-06-2008 , 01:13 PM






Hi,

I used the Nz() function in a query to replace blank entries with zero
so that a subsequent SUM function wouldn't trip over. However, it
meant that I couldn't format the resulting number when displayed in a
form. Is this a feature of the Nz() function or am I doing something
wrong.

Thanks,

Franc.

Reply With Quote
  #2  
Old   
Allen Browne
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-06-2008 , 11:37 PM






In queries, IIf() is better than Nz().

Instead of:
Nz([Amount], 0)
use:
IIf([Amount] Is Null, 0, [Amount])
Access will then understand the data type correctly.

The issue is that Nz() returns a Variant, which has a meaningful subtype in
VBA but is not recognised by JET. Consequently JET treats it as text by
default. IIf() does not have this problem.

An alternative is to explicitly typecast the result of Nz(), e.g.:
CCur(Nz([Amount],0))
This is generally less efficient, but sometimes handy.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"franc sutherland" <franc.sutherland (AT) googlemail (DOT) com> wrote

Quote:
I used the Nz() function in a query to replace blank entries with zero
so that a subsequent SUM function wouldn't trip over. However, it
meant that I couldn't format the resulting number when displayed in a
form. Is this a feature of the Nz() function or am I doing something
wrong.


Reply With Quote
  #3  
Old   
Wayne Gillespie
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 12:10 AM



On Fri, 7 Mar 2008 14:37:16 +0900, "Allen Browne" <AllenBrowne (AT) SeeSig (DOT) Invalid>
wrote:

Quote:
In queries, IIf() is better than Nz().

Instead of:
Nz([Amount], 0)
use:
IIf([Amount] Is Null, 0, [Amount])
Access will then understand the data type correctly.

The issue is that Nz() returns a Variant, which has a meaningful subtype in
VBA but is not recognised by JET. Consequently JET treats it as text by
default. IIf() does not have this problem.

An alternative is to explicitly typecast the result of Nz(), e.g.:
CCur(Nz([Amount],0))
This is generally less efficient, but sometimes handy.
Another way to typecast is to force a numeric calculation.

eg. Nz([Amount])+0


Wayne Gillespie
Gosford NSW Australia


Reply With Quote
  #4  
Old   
lyle fairfield
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 05:24 AM



franc sutherland <franc.sutherland (AT) googlemail (DOT) com> wrote in news:f97e269a-
80ee-4997-bf12-68edb00c1b73 (AT) c33...oglegroups.com:

Quote:
I used the Nz() function in a query to replace blank entries with zero
so that a subsequent SUM function wouldn't trip over.
Could you tell us of a situation where SUM trips over nulls.

Quote:
However, it
meant that I couldn't format the resulting number when displayed in a
form. Is this a feature of the Nz() function or am I doing something
wrong.
It's not a feature of the Nz Function.


Reply With Quote
  #5  
Old   
lyle fairfield
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 05:34 AM



"Allen Browne" <AllenBrowne (AT) SeeSig (DOT) Invalid> wrote in
news:47d0d48d$0$23639$5a62ac22 (AT) per-qv1-newsreader-01 (DOT) iinet.net.au:

Quote:
In queries, IIf() is better than Nz().

Instead of:
Nz([Amount], 0)
use:
IIf([Amount] Is Null, 0, [Amount])
Access will then understand the data type correctly.

The issue is that Nz() returns a Variant, which has a meaningful
subtype in VBA but is not recognised by JET. Consequently JET treats
it as text by default. IIf() does not have this problem.
What does IIf return? If it returns, say a double, then it might not treat
IIf(True, "A", "B") well at all. If it returns a variant (and the VBA IIF
does return a variant, but we know that the VBA and the JET-SQL IIF are not
identical) then it's likely JET could deal with its return value.


Reply With Quote
  #6  
Old   
franc sutherland
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 08:06 AM



On Mar 7, 5:37 am, "Allen Browne" <AllenBro... (AT) SeeSig (DOT) Invalid> wrote:
Quote:
In queries, IIf() is better than Nz().

Instead of:
Nz([Amount], 0)
use:
IIf([Amount] Is Null, 0, [Amount])
Access will then understand the data type correctly.

The issue is that Nz() returns a Variant, which has a meaningful subtype in
VBA but is not recognised by JET. Consequently JET treats it as text by
default. IIf() does not have this problem.

An alternative is to explicitly typecast the result of Nz(), e.g.:
CCur(Nz([Amount],0))
This is generally less efficient, but sometimes handy.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users -http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"franc sutherland" <franc.sutherl... (AT) googlemail (DOT) com> wrote in message

news:f97e269a-80ee-4997-bf12-68edb00c1b73 (AT) c33g2000hsd (DOT) googlegroups.com...



I used the Nz() function in a query to replace blank entries with zero
so that a subsequent SUM function wouldn't trip over. However, it
meant that I couldn't format the resulting number when displayed in a
form. Is this a feature of the Nz() function or am I doing something
wrong.
Hi Allen,

Thanks for that. Using IIf worked a treat.

All the best,

Franc.


Reply With Quote
  #7  
Old   
franc sutherland
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 08:07 AM



On Mar 7, 6:10 am, Wayne Gillespie <best... (AT) NOhotmailSPAM (DOT) com.au>
wrote:
Quote:
On Fri, 7 Mar 2008 14:37:16 +0900, "Allen Browne" <AllenBro... (AT) SeeSig (DOT) Invalid
wrote:

In queries, IIf() is better than Nz().

Instead of:
Nz([Amount], 0)
use:
IIf([Amount] Is Null, 0, [Amount])
Access will then understand the data type correctly.

The issue is that Nz() returns a Variant, which has a meaningful subtype in
VBA but is not recognised by JET. Consequently JET treats it as text by
default. IIf() does not have this problem.

An alternative is to explicitly typecast the result of Nz(), e.g.:
CCur(Nz([Amount],0))
This is generally less efficient, but sometimes handy.

Another way to typecast is to force a numeric calculation.

eg. Nz([Amount])+0

Wayne Gillespie
Gosford NSW Australia
Hi Wayne,

Thanks for your solution. It worked great.

All the best,

Franc.


Reply With Quote
  #8  
Old   
franc sutherland
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 08:09 AM



On Mar 7, 11:24 am, lyle fairfield <lylef... (AT) yah00 (DOT) ca> wrote:
Quote:
franc sutherland <franc.sutherl... (AT) googlemail (DOT) com> wrote in news:f97e269a-
80ee-4997-bf12-68edb00c1... (AT) c33g2000hsd (DOT) googlegroups.com:

I used the Nz() function in a query to replace blank entries with zero
so that a subsequent SUM function wouldn't trip over.

Could you tell us of a situation where SUM trips over nulls.

However, it
meant that I couldn't format the resulting number when displayed in a
form. Is this a feature of the Nz() function or am I doing something
wrong.

It's not a feature of the Nz Function.
Hi Lyle,

Sorry, my mistake there. It wasn't using the SUM function within a
query which caused the problem. It was adding the fields together in
a subsequent form which gave '#error'. Does that make more sense?

All the best,

Franc.


Reply With Quote
  #9  
Old   
lyle fairfield
 
Posts: n/a

Default Re: nz for blanks in queries - formatting - 03-07-2008 , 09:00 AM



franc sutherland <franc.sutherland (AT) googlemail (DOT) com> wrote in
news:299c6a9f-d998-4b77-b7f6-33c577fc51da (AT) x30g2000hsd (DOT) googlegroups.com:

Quote:
On Mar 7, 11:24 am, lyle fairfield <lylef... (AT) yah00 (DOT) ca> wrote:
franc sutherland <franc.sutherl... (AT) googlemail (DOT) com> wrote in
news:f97e269a-
80ee-4997-bf12-68edb00c1... (AT) c33g2000hsd (DOT) googlegroups.com:

I used the Nz() function in a query to replace blank entries with
zero so that a subsequent SUM function wouldn't trip over.

Could you tell us of a situation where SUM trips over nulls.

However, it
meant that I couldn't format the resulting number when displayed in
a form. Is this a feature of the Nz() function or am I doing
something wrong.

It's not a feature of the Nz Function.

Hi Lyle,

Sorry, my mistake there. It wasn't using the SUM function within a
query which caused the problem. It was adding the fields together in
a subsequent form which gave '#error'. Does that make more sense?

All the best,

Franc.

Much


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.