Results 1 to 9 of 9
  1. #1
    vad77 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Location
    USA
    Posts
    105

    Filter form based on DateDiff statement

    The form I created has a field called "expire" which is a date field. I created a button to insert the current date. To the right of the expire filed is a textbox that has a DateDiff statement to count the number of days from the expire date.
    =DateDiff("d",[expire],Now())
    This all works great, my next task is to be able to filter any records that has a expire date of 30 days or greater. I have tried several different code on a command button with different results and never anything close to what I need filtered.
    My statement eaither set a filter that still shows all records and another statement that filters down to 1 blank record.
    I have tried the following...

    Private Sub cmd_grtthirty_Click()
    Me.Filter = Text88 > 29
    Me.FilterOn = True
    End Sub

    and

    Private Sub cmd_grtthirty_Click()
    Me.Filter = DateDiff("d", Now, Expire) > 29
    Me.FilterOn = True
    End Sub

    I need to filter on this criteria so I can delete these records. This will be easier to locate records instead of looking for records which match contional format.
    Any help with this will be greatly appriciated.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    These are untested but maybe you can incorporate some of this to your form. Not sure what the field name is. I used expire in this example.

    Code:
    Dim dtWarn as Date
    dtWarn = Date + 30 'Determine the date for warnings
    Dim strFilter as String
    strFilter = "[expire] >= #" & dtWarn & "#"
    
    Me.filterOn = False
    Me.Filter = ""
    Me.Filter = strFilter
    Me.filterOn = True
    This example should filter expirations between now and 30 days. I normally adjust the form's recordset but I believe between will work on the .Filter property.
    Code:
    Dim strShow As String
    Dim dtWarn As Date
    Dim dtToday As Date
    Dim strFilter as String
    
        dtWarn = Date + 30 'Determine the date for warnings
        dtToday = Date
    
    strFilter = " Between " & "#" & dtToday & "#" & " And " & "#" & dtWarn & "#"
    
    Me.filterOn = False
    Me.Filter = ""
    Me.Filter = strFilter
    Me.filterOn = True
    Maybe you could combine the two and use a toggle button to switch between the two filters.

  3. #3
    vad77 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Location
    USA
    Posts
    105
    I tried both and they did not work. I left out a piece of information not all my records will have a date in the "expire" field only the records we are which we specify to watch when we enter a date. The rest of the records wll have a Null value. This database was built off of an excel spreadsheet. When a record was in question for renewal they highlighted in red. Then when it was time to clean up these records the filtered on the cell colr, red. Then they just deleted the row. In moving to Access I figured I put an field called "expire". If a record is to be watched for deletion the command button would enter the current date to start the countdown to deletion. I then added an unbound text box with a DateDiff statement to start counting the days from the expire date.
    What I want to do is just on any record that has an expire date of 30 days or more.

    Click image for larger version. 

Name:	expirerange.jpg 
Views:	13 
Size:	35.9 KB 
ID:	15198

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    maybe some where criteria then

    strFilter = "[expire] >= #" & dtWarn & "# WHERE not ([expire]) is null"

    Try that. I think I got the parentheses correct.

  5. #5
    vad77 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Location
    USA
    Posts
    105
    @ItsMe, thanks for taking the time to respond to my post.
    I am thiking I am making this to compicated than it needs to be. I think just applying a filter on any record that has a date in the expire field would be better. I know I can just navigate to a record where the expire field is null and use the filter by non-blank
    I think having a button on the form would be easier.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    yah, I am not focusing on the big picture. I am offering filter options based on your request and experiences I have had in the past.

    Let us know if you need further help. I don't think I see a question in post #5

  7. #7
    vad77 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Location
    USA
    Posts
    105
    OK, thanks!

  8. #8
    vad77 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Location
    USA
    Posts
    105
    Just an update...
    This what I did to be able to filter on any record with a date. It might not be pretty but it gives the result I was looking for. I added a true/false field to my contractor table. Then I modified my command but to set that value to True and add the current date in the field named "expire". Next I added a command button that applies a filter on the field "marked" where the value is set to True.
    The trouble I had was trying to apply a filter on a field that has different dates or a Null value.

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I use Booleans to distinguish the status of records. One thing to remember about Booleans is they are not the smallest data type in the tool box.

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

Similar Threads

  1. Replies: 5
    Last Post: 01-16-2013, 11:51 AM
  2. Replies: 1
    Last Post: 03-05-2012, 07:35 PM
  3. How to Filter a Form Based on checkmarks?
    By Silverhand4e in forum Forms
    Replies: 3
    Last Post: 01-09-2012, 04:47 PM
  4. filter form based on combo box
    By lloyddobler in forum Forms
    Replies: 8
    Last Post: 09-10-2009, 07:33 AM
  5. Form Based Query/Filter
    By Micon in forum Access
    Replies: 0
    Last Post: 11-07-2008, 09:25 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