473,396 Members | 2,108 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.

MS Access: Query rejects sorts when calculations are made field with null value

David Blackman
I have a query that shows date of birth from a field and calculates age as a variant with a simple function:

[Age]: fn_Age([DOB])

If any record in the table contains a null [DOB], the query rejects all sorts based on the [Age] field.

I’ve tried using the ‘Is Not Null’ criteria on [DOB], but still get the “Data type mismatch in criteria expression” error.

Any suggestion?
Jan 30 '16 #1
3 960
Stewart Ross
2,545 Expert Mod 2GB
One way to ensure that the [Age] field aliasing the function call always has a numeric value is to wrap the function call inside an Nz statement which returns a specified value if the inner statement is null. As Nz would return a value treated as text to the query this in turn has to be wrapped in another function such as Val or CLng to convert the text value to a whole-number:

Expand|Select|Wrap|Line Numbers
  1. Age: CLng(Nz(fn_Age([DOB], 0)))
In the statement above a 0 is returned if fn_Age returns a null. Sorting on such a value will place unknown ages first in the sort if in ascending order. If you want these to be last in the sort order, or to be flagged in an unambiguous way, you could just use a placeholder such as 999 for the age instead:

Expand|Select|Wrap|Line Numbers
  1. Age: CLng(Nz(fn_Age([DOB], 999)))
That way, all ages listed as 999 in value represent records with an unknown DOB.

Assuming that the null returned by fn_Age results from a null DOB, if you want to use the SQL IS NULL statement which you mention did not work correctly for you, you'd need to test the DOB field in an IIF statement like this:

Expand|Select|Wrap|Line Numbers
  1. [Age]: IIF([DOB] IS NULL, 999, fn_Age([DOB]))
Using IS NULL is a more general solution, partly because Nz is not an obvious function to call (the name does not identify what it does at all, unlike Val, say) and it is peculiar to MS VBA, so not easy to transfer if you need to port your Access SQL queries to other systems.

Lastly, you might find it better to resolve this issue at source in your fn_Age function code, by modifying the function so that it always returns an integer instead of a variant value. If it is [DOB] which may be null you could use the IsNull function within an IF statement in the VBA code for fn_Age to test for this and set or return an appropriate dummy value such as 0 or 999 in the same way:

Expand|Select|Wrap|Line Numbers
  1. If IsNull(DOBVariableName) Then
  2.    fn_Age = 999
  3. Else
  4.    {your existing calculation for DOB here}
  5. End If
-Stewart
Jan 30 '16 #2
zmbd
5,501 Expert Mod 4TB
+ David, what is omitted here is what exactly is it you want done with the sort order when ISNUll([DOB])=True

+ Personally, of the solutions offered by Stewart, [Age]: IIF([DOB] IS NULL, 999, fn_Age([DOB])) is the better option. :)

+ However, I don't like "false" values if they can be avoided; thus, another option, and this is a partial SQL from a production table I use, is to check for nulls, sort them first and then handle the date (or age in your case?):
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_EventHistory.EventHistory_pk
  2.    , tbl_EventHistory.event_datetime
  3. FROM tbl_EventHistory
  4. ORDER BY IsNull([event_datetime]) DESC
  5.    , tbl_EventHistory.event_datetime;
So, this will place all of the null value [Event_DateTime] at the bottom (true = -1 in Access). Either remove the DESC or change to ASC to group at the top.

Now, with [Age] being a calculated field, I'm not so sure that you can use the field name to replace [Event_DateTime] in this; however, isnull(fn_Age([DOB])) should work or you could just use the [DOB] as I have used [Event_DateTime] - I am guessing that [DOB] is a date/time field and I know that this construct works in my production database (active with more than a few hundred records :) )
Jan 31 '16 #3
Thanks for all the poop. I'm keeping your replies in OneNote for further reference.

What I'm doing is sorting ages into ranges; so if fn_Age returns a value that identifies the record as containing a null DOB, i.e. 999 as you suggest, I can simply use that value to exclude null records in the SQL WHERE clause.

You solved my problem. Thanks again. You folks are great. :)
Feb 2 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Puneet Murgai | last post by:
I am trying to run an access query which works when I write it directly in the database. However, it fails when an SQL script containing it is run from C++. It doesn't recognize the Iff and Nz...
5
by: msnews.microsoft.com | last post by:
Hello. How can I check if DataSet field value is Null?
3
by: Przemo | last post by:
Hi, Imagine that we have a database and one table. Stored procedure returns 3 columns of table: ID, name, value. Name and value can be a null in database. How to map or represent such values in...
2
by: Andrew | last post by:
hi, I got an error when the dataset returns a null value. eg. string a; a = (string)ds.Tables.Rows; How do I handle this error ? Thanks. regards,
1
by: accessbee | last post by:
(Sorry this is so long, was trying to fully explain the situation) There are two tables that handle information on our clients. Every client has a unique ClientID, and the ClientID is the Primary Key...
3
by: Akhenaten | last post by:
I have the following function which works great except when I encounter a NULL value in my array ($aidlook) and then I get "wrong data type" errors. I thought about doing a NULL check and creating...
2
by: Comandur | last post by:
Hi, I am trying to export an access query to excel. I have made use of transferspreadsheet command to achive this. However i have hardcoded the path and the filename in the VBA code. I am not sure...
5
by: laredotornado | last post by:
Hi, Is there a way to trigger an event when the value of a hidden field changes? At a certain point in time my hidden field is getting changed to a value that I don't want but I can't find a...
7
by: monika varshney | last post by:
I have to read a database using dataset. My database has some null values. My code is working fine when there is a value in the database. But when there is a null value in the database, dataset...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...
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.