Results 1 to 11 of 11
  1. #1
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65

    Query by date criteria help

    Hello,

    I have queries with criteria set to [Enter Date: MM/DD/YYYY] in order to have users search by date. If they don't type the date correctly, how do I ensure they must retype the date if it's incorrect? Or show all records instead of an empty report.



    I was thinking an if statement where Iif(Not IsNull(), [Enter Date], "*")

    But I'm sure I'm doing that wrong.. Help is greatly appreciated! Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you can use a form. on the form put a text box, txtDate
    set it for date format. it will give an error if they dont enter a date correctly.

    the query
    select * from table where [date] = forms!myForm!txtDate

  3. #3
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65
    That's not going to work for what I'm doing, although it's a good idea.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Why won't a form work?

    Inability to validate input in popup is 1 reason I NEVER use dynamic parameterized queries at all.

    You are not correctly using the IsNull() function and really shouldn't use it in this context.

    Also, wildcard is meaningless without LIKE operator.

    WHERE [fieldname] LIKE Iif(Not [Enter Date] Is Null, [Enter Date], "*")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65
    Ahh I see, thank you

  6. #6
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65
    So I've ended up getting it to work with that info.
    This works great!:
    [tsdate] LIKE Iif(NOT [Enter Date: MM/DD/YYYY] Is Null, [Enter Date: MM/DD/YYYY], "*")

    Works great for selecting a single day!

    I'm trying to do a date range now, and this doesn't seem to be working.
    How would I do this in a way that would allow me to return an entire month's range?

    I'm trying something like this, but it doesn't do the trick
    [tsdate] LIKE >= Iif(NOT [Enter Start Date: MM/DD/YYYY] Is Null, [Enter Start Date: MM/DD/YYYY], "*") and [tsdate] LIKE <= Iif(NOT [Enter End Date: MM/DD/YYYY] Is Null, [Enter Start End: MM/DD/YYYY], "*")

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Can't mix LIKE with other (=, <, >, etc) operators.

    Instead of LIKE and wildcard, provide an alternate date.

    [tsdate] BETWEEN Iif(NOT [Enter Start Date: MM/DD/YYYY] Is Null, [Enter Start Date: MM/DD/YYYY], #1/1/1900#) AND Iif(NOT [Enter End Date: MM/DD/YYYY] Is Null, [Enter Start End: MM/DD/YYYY], #12/31/2199#)



    Why can't you use a form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65
    It looks liek that isn't quite doing it either. I'm trying something different than range.


    This lets me select the month and it works great!
    Year([tsdate])=Year(Now()) And Month([tsdate])=[Enter Month (numeric)]


    How do I make it select year as well, since I wont always be looking for this year's reports?

    I'm trying this, but it doesn't work.
    Year([tsdate])=Year([Enter Year:]) And Month([tsdate])=Month([Enter Month:])

  9. #9
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65
    Last question for the day, I promise lol.
    Figuring that part out will solve a lot of issues I'm having.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Year([tsdate])=[Enter Year:] And Month([tsdate])=[Enter Month Number:]
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    mcmcd99 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    65
    You're my hero.

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

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2015, 12:45 PM
  2. Replies: 20
    Last Post: 01-13-2015, 02:23 PM
  3. Replies: 3
    Last Post: 08-21-2012, 03:05 PM
  4. Query using Date() criteria
    By Bruce in forum Queries
    Replies: 27
    Last Post: 01-30-2012, 01:15 PM
  5. Query Date Criteria
    By svanicek in forum Access
    Replies: 1
    Last Post: 07-09-2010, 11:13 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