Results 1 to 10 of 10
  1. #1
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25

    Cool Query Between Dates with an additional condition

    Hi, guys! New to this forum.

    Ok I hope I can explain this simply enough.

    I have a Form that generates a simple Weekly Report. On this form I have two Text Boxes with a [txtDateFrom] and [txtDateTo]. My week starts Thursday
    at 2pm and ends the next Thursday at 2pm (7 days).

    My query attached to this Form has the Between Dates in the criteria and this works perfectly except I want to be able to get all records after 2pm on
    Thursday and before 2pm the next Thursday. However when I originally entered the data into the table I didn't set the date to time as well and instead made a column of After 2pm and in that column anything done After 2pm on Thursday has "After 2pm". Clearly now I know I should not have done this, but be that as it is, is there anyway to program getting the report to only grab the records from Thursday After 2pm to Thursday Before 2pm?

    Here is an example of my Query Data Sheet:

    Date of Transaction | Type | Amount | After 2pm | Branch |
    7/4/2019 CHECK $150.00 TLOC
    7/4/2019 CHECK $150.00 TLOC
    7/4/2019 CASH $230.00 After 2pm TBOD
    7/4/2019 CASH $230.00 After 2pm TBOD
    7/4/2019 CASH $230.00 After 2pm TBOD
    7/4/2019 CHECK $150.00 TLOC
    7/5/2019 CHECK $150.00 TLOC
    7/6/2019 CHECK $150.00 TLOC
    7/7/2019 CHECK $150.00 TLOC


    7/8/2019 CHECK $150.00 TLOC
    7/9/2019 CHECK $150.00 TLOC
    7/10/2019 CHECK $150.00 TLOC
    7/11/2019 CHECK $150.00 TLOC
    7/11/2019 CHECK $150.00 After 2pm TLOC

    When I run the query I only want the above to show the ALL records from Thursday After 2pm to Thursday before 2pm as shown here:

    7/4/2019 CASH $230.00 After 2pm TBOD
    7/4/2019 CASH $230.00 After 2pm TBOD
    7/4/2019 CASH $230.00 After 2pm TBOD
    7/4/2019 CHECK $150.00 TLOC
    7/5/2019 CHECK $150.00 TLOC
    7/6/2019 CHECK $150.00 TLOC
    7/7/2019 CHECK $150.00 TLOC
    7/8/2019 CHECK $150.00 TLOC
    7/9/2019 CHECK $150.00 TLOC
    7/10/2019 CHECK $150.00 TLOC
    7/11/2019 CHECK $150.00 TLOC

    Is that possible? Or do I then have to change my table completely.

    Please help! Thanks!

    Regards, Phil

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    I highly recommend you fix this problem now rather than put a band-aid on it. Band-aids have a way of coming off.
    What I would do is copy the table then create a new table field set for date AND time. Use an update query to set this field to the date + #02:01:00 PM# where you have "After 2pm" in the record. Then set the remainder to 01:01:00 PM. Seems obvious that the exact time is not important to your records. Going forward, new records get the complete date/fime. Then it is a simple matter of > and < a date and time that you specify in a query. You can also add comments in table design to give some clue to what will be odd looking data in the current set of records.
    If you do not, I'd say your troubles are just beginning

    EDIT - Forgot to say that you'd probably want to dump the "After 2PM" field in the new table, but I have no idea what effect that may have on our queries, forms and reports. I suspect that they will be useless as is since they would not be linked to this new date field. Still, it's the right way to go.
    Last edited by Micron; 07-20-2019 at 09:00 AM. Reason: correction and comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25
    Here is the SQL for the query.

    Code:
    SELECT DISTINCTROW [INCOMING MONIES].DATETR, [INCOMING MONIES].TYPE, [INCOMING MONIES].AMOUNT, [INCOMING MONIES].[DEP AMOUNT], [INCOMING MONIES].AF2, [INCOMING MONIES].ORG
    FROM [INCOMING MONIES]
    GROUP BY [INCOMING MONIES].DATETR, [INCOMING MONIES].TYPE, [INCOMING MONIES].AMOUNT, [INCOMING MONIES].[DEP AMOUNT], [INCOMING MONIES].AF2, [INCOMING MONIES].ORG
    HAVING ((([INCOMING MONIES].DATETR) Between [Forms]![ReportGen]![DateFromTxt] And [Forms]![ReportGen]![DateToTxt]))
    ORDER BY [INCOMING MONIES].DATETR;

  5. #5
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25
    Thank you for this. I posted my SQL.

    I would take your advice but a) I am not that knowledgable yet with creating this new table with that format, b) I currently have a from that is linked to this query and table and 2 reports and don't want to mess anything up as it is quite a big database and c) the After 2pm entries are is very few far and between and only occur on Thursday of each week, if at all. If there is anyway I could just figure out a WHERE clause or something that takes Thursday (After 2pm) entries to Thursday (ignoring the After 2pm's) entries. That way I can get my reports to report the correct figures for the WEEK.

    Thanks a million.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    I agree with Micron's advice. Fix the structural problem - the sooner the better. The longer you leave this issue which is the root of your problem, the more difficult to change and correct existing data.

    I know you know this, but to emphasize:
    -model your database structure with test data and test scenarios for output, inputs and logic
    -involve others in vetting the model to ensure broad scope and independent opinions
    -build a prototype, and vet again to ensure coverage.

    As for the WHERE clause and your current question:

    -take some data that is of concern ( only a few records that illustrate your issue)
    -in English, write down the steps you think would identify the records you want to change/edit adjust
    -post the steps for review by forum participants OR
    -------make a separate database, same tables limited records
    -------make a list of the records that you feel should be in the result(expected values)
    -------and try your evolving WHERE clause and see what you actually get (observed values)
    -------compare expected and observed
    -------you'll learn more by working through and resolving the issue

    Good luck.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    not wanting to see you stuck when you come for help, consider this WHERE part at least.
    Code:
    SELECT DISTINCTROW IM.DATETR, IM.TYPE, IM.AMOUNT, IM.[DEP AMOUNT], IM.AF2, IM.ORG FROM [INCOMING MONIES] AS IM 
    WHERE WHERE DATETR >= [Forms]![ReportGen]![DateFromTxt] AND AF2 = "After 2pm" OR DATETR <= [Forms]![ReportGen]![DateToTxt] ORDER BY IM.DATETR;
    I don't see the point in grouping if there are no aggregate functions being used. HAVING is slower than WHERE, which may not be an issue based on your table size.
    I may have forgotten or misinterpreted the way it's supposed to work but perhaps that will point you in the right direction.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    And while you are fixing things,

    You should think about removing all spaces in object names.
    Code:
    [INCOMING MONIES].[DEP AMOUNT], '<<-- spaces in field name and table name
    
    [INCOMING MONIES].TYPE,        '<<-- "TYPE" is a reserved word in Access and shouldn't be used for object names.

    Suggestions:
    --------------------
    Object names should be letters and numbers.
    Do not begin an object name with a number.
    NO spaces, punctuation or special characters (exception is the underscore) in object names


    And here is a list of Reserved words

  9. #9
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25
    Hey guys, Thanks for your help.

    Micron your code helped and from there I was able to figure it out. Here is the final code that works:

    Code:
    SELECT DISTINCTROW IM.[#], IM.DATETR, IM.ORG, IM.TYPE, IM.SERIES, IM.[INV#], IM.[FIN DEP #], IM.[BATCH/TRN #], IM.AMOUNT, IM.[DEP AMOUNT], IM.AF2FROM IM
    WHERE (IM.[DATETR] = [Forms]![Input Transactions]![WkFromTxt] AND IM.[AF2] is not null)
    OR
    (IM.[DATETR] = [Forms]![Input Transactions]![wkToTxt] AND IM.[AF2] is null)
    OR
    (IM.[DATETR] > [Forms]![Input Transactions]![WkFromTxt] AND IM.[DATETR] < [Forms]![Input Transactions]![wkToTxt])
    ORDER BY IM.DATETR;
    Also thank you Orange -- I tried that route too but I was always finding that I would have to put 1pm each time I entered in a record that was not before 2pm and it was too much for me as there are only 1 or 2 records where After 2pm applied. Otherwise it did work. It is just easier for me what Micron suggested, and due to the fact that it is just a database for me, I thought it would be better.

    Steve, I didn't see your answer until just now and I will heed to it from now on.

    Thanks a lot guys!

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    You are very welcome. We're all happy to assist.
    Thanks for getting back.

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

Similar Threads

  1. Replies: 2
    Last Post: 11-22-2018, 05:34 AM
  2. Replies: 5
    Last Post: 01-11-2016, 11:43 AM
  3. Where Condition Problem - Dates
    By soco3594 in forum Programming
    Replies: 2
    Last Post: 10-20-2015, 02:11 PM
  4. Query with many criteria (Dates, Condition, etc)
    By soldat452002 in forum Queries
    Replies: 5
    Last Post: 06-25-2014, 05:01 PM
  5. docmd.open form between dates condition
    By Ruegen in forum Programming
    Replies: 6
    Last Post: 09-22-2013, 11:23 PM

Tags for this Thread

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