11

The following query is not returning values for CurrentVisitor in my ms access 2010 database:

SELECT h.ClientNumber, IIf(h.CheckoutDate=null,"Yes","") AS CurrentVisitor 
FROM VisitsTable AS h 
INNER JOIN (
    SELECT ClientNumber, MAX(LastVisitDate) AS LastVisitStart 
    FROM VisitsTable 
    GROUP BY ClientNumber)  
    AS t 
ON (h.LastVisitStart = t.LastVisitStart) AND (h.ClientNumber = t.ClientNumber);

I think the reason is that the check for null in the If() operation is not written correctly. Can anyone show me how to fix this?

1 Answer 1

28

Use

Is Null

rather than

= Null
Sign up to request clarification or add additional context in comments.

5 Comments

+1. And credit for the answer. Thank you for the quick and correct response.
IIf(IsNull(...), ..., ...) would have been the more traditional way to do it, but if Is Null works then who am I to quibble?
@GordThompson - why do you say 'more traditional'? Unless things have changed, 'Something Is Null' is pure SQL, 'IsNull(Something)' (in a JET context) VBA, and it's always better to use pure SQL if you can.
IIf() is already fairly specific to Access SQL and VB* (compare with CASE ... WHEN ... in T-SQL) so IsNull() is how old timers like me would tend to write it. But like I said, "If it works..." (and I've already upvoted your answer).
@GordThompson - sure, we're dealing with Access SQL, but that still doesn't impact my preference for Is Null over IsNull (that's why I put 'in a JET context'). Moreover, the Access SQL IIf is different from the Access VBA IIf, as I expect you know (the SQL one short circuits, the VBA one - rather annoyingly - doesn't).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.