+ Reply to Thread
Results 1 to 7 of 7

Criteria for query won't accept weekday function with OR statement

Hybrid View

  1. #1
    Registered User
    Join Date
    11-04-2015
    Location
    Memphis, Tennesee
    MS-Off Ver
    Office 2013
    Posts
    5

    Criteria for query won't accept weekday function with OR statement

    Hi All,

    I'm trying to get the following criteria to work in a date field: basically, if today is a Sunday or Monday, I want to show some sales from the previous week. If not, I want to show today's and yesterday's sales.

    The following is the part in the criteria row, with the problem part highlighted.

    IIf(Weekday(Date())=2 Or Weekday(Date())=1,Between (Date()-3) And (Date()),Between (Date()-1) And (Date()))

    I know the "true" and "false" parts work - i've tested them separately without the iif statement and I get results for both. The expression Weekday(Date())=7 Or Weekday(Date())=1 seems to be the issue - with the whole statement, I get no entries after I run the query - it results in a blank query. For some reason, it isn't choosing either of the true or false options.

    Shouldn't the iif statement choose one of my two options? If it doesn't choose either, shouldn't it tell me there's an error?

    Is there something wrong with how I'm using the OR operator in an iif statement?

    Thanks,
    Daniel

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Criteria for query won't accept weekday function with OR statement

    cant you just say

    IIF(Weekday(Date()) < 3
    instead of the OR?
    If someone has helped you then please add to their Reputation

  3. #3
    Registered User
    Join Date
    11-04-2015
    Location
    Memphis, Tennesee
    MS-Off Ver
    Office 2013
    Posts
    5

    Re: Criteria for query won't accept weekday function with OR statement

    Well, I hoped that would work pjwhitfield but for whatever reason I'm still getting nothing.

    I used: IIf((Weekday(Date()))<6,Between (Date()-3) And (Date()),Between (Date()-1) And (Date()))

    I checked the "true" and "false" results again just to check, and they both worked by themselves. So (Weekday(Date()))<6 is the problem.

    Hmm, this is kinda strange - the following criteria works fine in the date field: IIf(Weekday(Date())<6,42389,42390).

    As soon as I threw in a between and and, it failed: IIf(Weekday(Date())<6,(Between 42389 And 42390),42389). I got a blank worksheet.

    So you can't use a between and and operator as part of the output of an iff function?

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Criteria for query won't accept weekday function with OR statement

    I think we would need to see exactly what youre doing not just the IIF statement, if this is a query then can you post the complete SQL?

  5. #5
    Registered User
    Join Date
    11-04-2015
    Location
    Memphis, Tennesee
    MS-Off Ver
    Office 2013
    Posts
    5

    Re: Criteria for query won't accept weekday function with OR statement

    Certainly.

    Here's the SQL that doesn't work:

    SELECT TBL_Sales.Sales_Order_Number, TBL_Sales.Sales_Order_Date, TBL_Sales.Type, TBL_Sales.Finish, TBL_Sales.Length, TBL_Sales.Options, TBL_Sales.Quantity, TBL_Sales.Model_Number_Lookup
    FROM TBL_Sales
    WHERE (((TBL_Sales.Sales_Order_Date)=IIf(Weekday(Date())<6,(TBL_Sales.Sales_Order_Date) Between 42389 And 42390,42390)))
    ORDER BY TBL_Sales.Sales_Order_Date;

    Here's the SQL that does work:

    SELECT TBL_Sales.Sales_Order_Number, TBL_Sales.Sales_Order_Date, TBL_Sales.Type, TBL_Sales.Finish, TBL_Sales.Length, TBL_Sales.Options, TBL_Sales.Quantity, TBL_Sales.Model_Number_Lookup
    FROM TBL_Sales
    WHERE (((TBL_Sales.Sales_Order_Date)=IIf(Weekday(Date())<6,42389,42390)))
    ORDER BY TBL_Sales.Sales_Order_Date;

    I tried to bold the only significant difference that I saw.

  6. #6
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Criteria for query won't accept weekday function with OR statement

    In your working one you're effectively saying:
    WHERE (((TBL_Sales.Sales_Order_Date)=42389))
    When its true however the failing one would equate to
    WHERE (((TBL_Sales.Sales_Order_Date)=(TBL_Sales.Sales_Order_Date) Between 42389 And 42390))
    so you're saying WHERE Sales_Order_Date = Sales_Order_Date Between 42389 And 42390

    not sure if this will work but try:
    WHERE ((IIf(Weekday(Date())<6,(TBL_Sales.Sales_Order_Date) Between 42389 And 42390,(TBL_Sales.Sales_Order_Date) = 42390)))
    (pretty sure Ive got the brackets wrong but you can see the gist of it.

  7. #7
    Registered User
    Join Date
    11-04-2015
    Location
    Memphis, Tennesee
    MS-Off Ver
    Office 2013
    Posts
    5

    Re: Criteria for query won't accept weekday function with OR statement

    Well, I can't complain - copying and pasting the where clause into the SQL worked perfectly! Thanks so much.

    But I don't exactly know what it's doing.

    Instead of adding information to the criteria part of the field Sales_Order_Date, it added a calculated field. The field has IIf(Weekday(Date())<6,([TBL_Sales].[Sales_Order_Date]) Between 42389 And 42390,([TBL_Sales].[Sales_Order_Date])=42390) as an expression for the field, with the "Show" checkbox unchecked, and the criteria of <> False

    If you've got the time, could you elaborate on what it's doing?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel VBA code to return weekday name from a date then autofilters for userdefined weekday
    By studyengineering in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2016, 03:24 PM
  2. Count Weekday but filter by 2 criteria
    By quadra in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-20-2015, 07:08 PM
  3. [SOLVED] SUMIFS Function with an OR Statement In One Criteria
    By nevi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-20-2014, 03:31 AM
  4. [SOLVED] Using WEEKDAY() as a criteria in AVERAGEIFS()
    By carwashguy in forum Excel General
    Replies: 6
    Last Post: 09-25-2013, 02:57 PM
  5. If Statement with two criteria and a matching function
    By adamsiskin in forum Excel General
    Replies: 5
    Last Post: 08-27-2012, 04:45 PM
  6. If statement using weekday to give result
    By jsmity in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-04-2008, 08:39 PM
  7. [SOLVED] adv filter wont accept criteria
    By daniel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-03-2006, 04:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1