473,387 Members | 1,374 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,387 software developers and data experts.

Query problems when using Nz

Hi all,

I am trying to get this query right, but so far no joy...

I have written the following code in the Field box of the QBD grid of
a query:

firstvalue:IIf(Not (IsNull([Value1])),[Value1],(IIf(Not
(IsNull([Value2])),[Value2],(IIf(Not
(IsNull([Value3])),[Value3],[Value4])))))

What I actually want in field "firstvalue" is the left most value of
Value1, Value2, Value3 and Value4. E.g If for a record Value1 = 0 (or
NULL), then it must return Value2 or Value3 or Value4, depending on
the first one which is not 0 (or NULL).

I keep getting syntax errors, and specifically pointing to the first
comma in the code.
Can somebody please tell me what is wrong? I can email a sample DB to
someone if they want to check out the table which the query is based
on.

Thanks a bunch, in advance!

JP
Nov 13 '05 #1
4 1781
On 5 Aug 2004 01:04:12 -0700, je**********@hotmail.com (Jean) wrote:

Proceed like this, so you can maintain your sanity. Don't try to write
the statement at once, but in several passes. In this example I'm
using "x" as a placeholder for future work.

Use Nz to convert both 0 and null values to 0.

If the Nz=0, do something I'll specify later, else return Value1:
FirstValue: iif(Nz(Value1)=0,x,Value1)

'Here I'm filling in "x" for the second pass.
FirstValue: iif(Nz(Value1)=0,iif(Nz(Value2)=0,x,Value2),Value1 )

You do the third and 4th pass, exactly the same way.

-Tom.

Hi all,

I am trying to get this query right, but so far no joy...

I have written the following code in the Field box of the QBD grid of
a query:

firstvalue:IIf(Not (IsNull([Value1])),[Value1],(IIf(Not
(IsNull([Value2])),[Value2],(IIf(Not
(IsNull([Value3])),[Value3],[Value4])))))

What I actually want in field "firstvalue" is the left most value of
Value1, Value2, Value3 and Value4. E.g If for a record Value1 = 0 (or
NULL), then it must return Value2 or Value3 or Value4, depending on
the first one which is not 0 (or NULL).

I keep getting syntax errors, and specifically pointing to the first
comma in the code.
Can somebody please tell me what is wrong? I can email a sample DB to
someone if they want to check out the table which the query is based
on.

Thanks a bunch, in advance!

JP


Nov 13 '05 #2
je**********@hotmail.com (Jean) wrote in message news:<7e*************************@posting.google.c om>...
Hi all,

I am trying to get this query right, but so far no joy...

I have written the following code in the Field box of the QBD grid of
a query:

firstvalue:IIf(Not (IsNull([Value1])),[Value1],(IIf(Not
(IsNull([Value2])),[Value2],(IIf(Not
(IsNull([Value3])),[Value3],[Value4])))))

What I actually want in field "firstvalue" is the left most value of
Value1, Value2, Value3 and Value4. E.g If for a record Value1 = 0 (or
NULL), then it must return Value2 or Value3 or Value4, depending on
the first one which is not 0 (or NULL).

I keep getting syntax errors, and specifically pointing to the first
comma in the code.
Can somebody please tell me what is wrong? I can email a sample DB to
someone if they want to check out the table which the query is based
on.

Thanks a bunch, in advance!

JP


HI:
Try to past the following into the SQL view window.
DO NOT INCLUDE ANY NEW LINE MARKERS. The following
is all on one line. ( maybe that is where you had the problem )?

Assumed Table name = T
Assuemd Table Fields: named Value1, Value2, etc.

SELECT IIf(Not (IsNull(T.[Value1])),T.[Value1],(IIf(Not
(IsNull(T.[Value2])),T.[Value2],(IIf(Not
(IsNull(T.[Value3])),T.[Value3],T.[Value4]))))) AS firstvalue
FROM T;

I tested this on Access 97 and it returned the
expected value. However, if you wanted something
other than the complete field value, that is another
fish to fry

John
Nov 13 '05 #3
je**********@hotmail.com (Jean) wrote in message news:<7e*************************@posting.google.c om>...
Hi all,

I am trying to get this query right, but so far no joy...


If you still have trouble after applying Tom's advice, try placing the
following code in a module:

Public Function GetFirst(var1 As Variant, var2 As Variant, var3 As
Variant, var4 As Variant) As Variant
GetFirstValue = 0
If Nz(var1) <> 0 Then
GetFirst = var1
ElseIf Nz(var2) <> 0 Then
GetFirst = var2
ElseIf Nz(var3) <> 0 Then
GetFirst = var3
ElseIf Nz(var4) <> 0 Then
GetFirst = var4
End If
End Function

Then:

SELECT GetFirst([Value1],[Value2],[Value3],[Value4]) AS FirstValue
FROM tblValues;

James A. Fortune
Nov 13 '05 #4
Thanks for all the help everyone! I actually sorted this one out late
yesterday afternoon. :)

The problem I had was that I am using a German version of MS Access,
and somehow it did not understand the term Iif ("Wenn" in German).
I used this statement in the field box of the QBD grid:

firstvalue: Wenn(nz([value1])<>0;[value1];Wenn(nz([value2])<>0;[value2];Wenn(nz([value3])<>0;[value3];[value4])))

Strange, cause it can translate other terms, like Nz, True, IsNull
etc.

Well I got it to work now and would like to thank you all for being so
kind in helping me.

Regards,

Jean

ja******@oakland.edu (James Fortune) wrote in message news:<a6**************************@posting.google. com>...
je**********@hotmail.com (Jean) wrote in message news:<7e*************************@posting.google.c om>...
Hi all,

I am trying to get this query right, but so far no joy...


If you still have trouble after applying Tom's advice, try placing the
following code in a module:

Public Function GetFirst(var1 As Variant, var2 As Variant, var3 As
Variant, var4 As Variant) As Variant
GetFirstValue = 0
If Nz(var1) <> 0 Then
GetFirst = var1
ElseIf Nz(var2) <> 0 Then
GetFirst = var2
ElseIf Nz(var3) <> 0 Then
GetFirst = var3
ElseIf Nz(var4) <> 0 Then
GetFirst = var4
End If
End Function

Then:

SELECT GetFirst([Value1],[Value2],[Value3],[Value4]) AS FirstValue
FROM tblValues;

James A. Fortune

Nov 13 '05 #5

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

Similar topics

13
by: dogu | last post by:
Noob alert. Code is below. File is saved as a .php. What I'm trying to do: User uses 'select' box drop down list to pick a value. Value ($site) is derived from a db query. This works fine....
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
6
by: carverk | last post by:
Hello All I'm in the middle of moving a MS Access DB to a MySql backend. I have figured out about 90% of the problems I have faced, execpt for this one. I have 3 Queries, which pull records...
2
by: Norm | last post by:
I have run into problems from time to time (and this is one of those times) using visual basic to access an external database and perform a basic select from statement. When the table is extremely...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
6
by: gerbski | last post by:
Hi all, I am relatively new to ADO, but up to now I got things working the way I wanted. But now I've run into somethng really annoying. I am working in MS Access. I am using an Access...
2
by: webhead74 | last post by:
Hi, I'm having intermittent problems with queries from my php script to a postgresql database. I have a form where I can enter a search query - for instance a last name. This leads to a...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
9
by: Bob Darlington | last post by:
The following query opens slowly the first time it is opened (6-7 seconds), but then is less than one second for the next random number of openings before slowing (6-7 seconds) again. SELECT...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.