Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    Trying to query dates


    I have a query where I've calculated the expiration dates to some classes. I'm trying to see only those classes that are coming up to expiration. I have a form with a text box. I want that text box to indicate how many months I want to search for an expiration. I want to see only those classes that are going to expire in [Variable] months. I'm having a hard time coming up with a way to do that.

  2. #2
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    This is what I've tried. I subtracted today's date from the date of Training: DaysPassed: [Date()]-[LastOfDOT]
    Then I took how many days till it expires and subtracted how many days passed which should give me how many days till it expires. Then dividing by 30 should give me the months till it expires: Int([DaysToExpire]-[DaysPassed]/30)
    I'm having a hard time after that though. On the query, I used the criteria: <= ([Forms]![frmSwitchboardTraining]![txtMonths]). I fill in the txtMonths with a number, and it doesn't calculate it right. However, if I put it directly into the query: <=4, it works perfectly.

  3. #3
    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,870
    Usually when looking for a difference in dates , you would use dateDiff() function.
    When you have a calculation [Int([DaysToExpire]-[DaysPassed]/30)] that deals with "months", you are in fact dealing with an approximation --since not all "months" have the same number of days.

    I'm having a hard time after that though
    Can you describe what you would like to do in plain, simple English --no jargon and no database terms.

  4. #4
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Thanks. I forgot about the DateDiff. I still have the same search problem thought. I'm not writing my criteria correctly. When I try to query "2" months, it gives it correctly.
    Click image for larger version. 

Name:	query1.jpg 
Views:	19 
Size:	57.2 KB 
ID:	34818
    Click image for larger version. 

Name:	query2.jpg 
Views:	14 
Size:	147.7 KB 
ID:	34819

    But when I add the search from a form, it will not give the correct query. In these, I'm still trying to query "2" but from a form.
    Click image for larger version. 

Name:	query3.jpg 
Views:	14 
Size:	55.3 KB 
ID:	34820
    Click image for larger version. 

Name:	query4.jpg 
Views:	16 
Size:	169.8 KB 
ID:	34821

    I'm obviously not writing the criteria correctly. Ideas?

  5. #5
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Copy the Criteria into a field in your query, see what it thinks it is.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I didn't understand. Copy the Criteria into a field in my query? The criteria is in the query.

  7. #7
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Yes copy it and paste it to the top line of your query designer, and the value will appear as a query field on every line, just for debugging purposes.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I'm still not following. Top line of my query designer? I don't understand this. Where? Go to SQL? I tried to put it in the field, table, and sort. Of course, it rejected it. I have the criteria in the first line of the criteria field. I don't know of another place to write it.

  9. #9
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Is there a way you can show a pic of what you're saying?

  10. #10
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Yes - something like this ;
    Click image for larger version. 

Name:	Criteria_As_Field.PNG 
Views:	13 
Size:	73.0 KB 
ID:	34822
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Click image for larger version. 

Name:	query1.jpg 
Views:	16 
Size:	59.8 KB 
ID:	34823
    Click image for larger version. 

Name:	query2.jpg 
Views:	16 
Size:	91.2 KB 
ID:	34824
    Click image for larger version. 

Name:	query3.jpg 
Views:	16 
Size:	169.6 KB 
ID:	34825

  12. #12
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Okay so now run that with your form open and a value in the form? and show us the result?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    In the MonthsLeft query field, where are you getting those values from? Can't see them in the rest of the query design, nor the table/query datasheet results.
    When the query is run, the form is open in form view, with the textbox containing a value?
    Edit: forgot to say that these two parts are contradictory

    I subtracted today's date from the date of Training:

    DaysPassed: [Date()]-[LastOfDOT]

    Your calculation is the opposite of your statement. It could be that this is why it works for "2" when you say
    I fill in the txtMonths with a number, and it doesn't calculate it right
    That statement doesn't help much.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Using the form for its value:
    Click image for larger version. 

Name:	query.jpg 
Views:	15 
Size:	185.2 KB 
ID:	34826

  15. #15
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I did a DateDiff to get that field:
    Click image for larger version. 

Name:	query.jpg 
Views:	14 
Size:	47.5 KB 
ID:	34827

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2017, 11:31 AM
  2. Replies: 5
    Last Post: 07-12-2014, 02:55 PM
  3. Replies: 5
    Last Post: 01-29-2014, 02:42 PM
  4. Replies: 1
    Last Post: 10-18-2013, 03:14 PM
  5. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM

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