Results 1 to 9 of 9
  1. #1
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109

    Search Date Range with Null Sources

    I have a start date control and an end date control to search a record source (date field). I found a thread with the expression to do the search but it doesn't completely work for me. The reason is I haven't populated all the table date fields (some are null) and when I don't have anything in start date and end date control it gives all the values without nulls. I would really like it to show all values including nulls. How would I adjust the expression to do so? Also would someone be able to explain the whole expression to me so I understand what is going on with it? I don't understand the portion that says [Forms]![DataEntry]![startdate] & ""="". I understand the true and false part of the if statement. Thank you for your help.

    https://www.accessforums.net/forms/s...orm-13232.html

    Code:
    Between IIf([Forms]![DataEntry]![startdate] & ""="",#1/1/1900#,[Forms]![DataEntry]![startdate]) And IIf([Forms]![DataEntry]![enddate] & ""="",Date(),[Forms]![DataEntry]![enddate])


  2. #2
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    I decided to not worry about getting the records with null values because it will be a required field when everything is set up. If someone could still explain the portion of the expression I am confused about, that would be great. Thank you.

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    You didn't say how you are using this, but if you are using it as part of a SQL statement in a string, you could try the Nz function instead of the Iif:


    "Between #" & nz([Forms]![DataEntry]![startdate], #1/1/1900# ) & "# And #" & nz([Forms]![DataEntry]![enddate],date()) & "#"

    The Nz functions say if startdate is null, use 01/01/1900, and if enddate is null, use the current date.

    This is one case where Nz is a lot easier to use than iif. A word of caution: if your default system date format is not mm/dd/yyyy, then you will have to format the dates to match .

    HTH

    John

  4. #4
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    Quote Originally Posted by John_G View Post
    Hi -

    You didn't say how you are using this, but if you are using it as part of a SQL statement in a string, you could try the Nz function instead of the Iif:


    "Between #" & nz([Forms]![DataEntry]![startdate], #1/1/1900# ) & "# And #" & nz([Forms]![DataEntry]![enddate],date()) & "#"

    The Nz functions say if startdate is null, use 01/01/1900, and if enddate is null, use the current date.

    This is one case where Nz is a lot easier to use than iif. A word of caution: if your default system date format is not mm/dd/yyyy, then you will have to format the dates to match .

    HTH

    John
    Thank you John. It is nice to learn a different way of doing the same thing. To answer your question, it is an expression in a query. I think I am going to keep the if statement because I believe it takes into account zero length strings as well. Those would be produced if someone deleted what they originally wrote in the field. I am confused at how the if statement works though. What I don't understand in the if statement is why the ""="" is added to the string. To take into account nulls and zero length strings, I would write something like if nz([field]) or [field] = "". I don't know how that is done by just saying if [field] & ""="".

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Since the query is referring to form fields, you don't need to worry about zero-length strings. Deleting all the contents of a control results in a Null. (If fact, for a text box, entering only blanks results in Null as well). A zero-length string cannot be entered by a user.

    However, if you want to check for both Null and zero-length at the same time, you can use nz([field],"").

    Now that you point it out, I cannot see what the IIf's do either - were you able to actually use them without error?

    John

  6. #6
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    I didn't get an error with it and I tested it by trying all combinations so it looks good.

  7. #7
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Now I see it - it makes perfect sense. What it does is add a zero-length string to the date value; if the date value is Null, then Null & "" = "".

    Adding brackets and spaces to the expression so that it is clearer gives:


    IIf( ([Forms]![DataEntry]![startdate] & "") = ("") ,#1/1/1900#,[Forms]![DataEntry]![startdate])

    And, it should work fine because it takes care of the nulls. Clever.

    Cheers!

    John

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Another common approach to identifying that a field is Null is

    Code:
      If Len(FieldInQuestion & "") = 0  Then  FieldInQuestion is Null
    in your example
    IIf(Len(([Forms]![DataEntry]![startdate] & "") = 0 ,#1/1/1900#,[Forms]![DataEntry]![startdate])

  9. #9
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    John - Thank you. I see it now. I was confused because I saw it as an equal in between two quotes instead of quotes equal to the other quotes.

    Orange - Thank you for your alternate example. That made it even more clear.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 07-18-2012, 10:13 PM
  2. Replies: 4
    Last Post: 05-26-2012, 09:29 AM
  3. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  4. Search form with a date range
    By mantro174 in forum Forms
    Replies: 1
    Last Post: 02-11-2009, 10:45 PM
  5. Numeric value out of range (null)
    By PPCMIS2009 in forum Queries
    Replies: 0
    Last Post: 02-03-2009, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums