FIltering on a form using just the year from a date field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beaker213
    New Member
    • Jul 2015
    • 2

    #1

    FIltering on a form using just the year from a date field

    I have created two unbound boxes on a form that users can enter name [cbonamefilter] and date [cmboyearfilter]. I want the form to filter by name and the "YYYY" part of the date. I have researched the forum and found most relative responses and tried to use suggested coding. The name portion of the filter works great but I'm having trouble getting the year part to work. I was successful when I was entered an exact date but not for just the year portion. I'm new to VBA so I'm not sure what I am missing. I get a runtime error when I try to run the code below.
    Code:
    Private Sub cbonamefilter_AfterUpdate()
    Call CheckFilter
    End Sub
    Private Sub cmboyearfilter_AfterUpdate()
    Me!cmboyearfilter = IIf(IsDate(Me!cmboyearfilter), _
                                      Format(Me!cmboyearfilter, "mm/dd/yyyy"), _ "")
    
    Call CheckFilter
    End Sub
    
    Private Sub CheckFilter()
    
    Dim strFilter As String, strOldFilter As String
    strOldFilter = Me.Filter
    
    'cbonamefilter - text
    If Me!cbonamefilter > "" Then _
    strFilter = strFilter & _
    " AND ([Initials-Pre] Like '" & _
    Me!cbonamefilter & "*')"
    
    'cmboyearfilter - date
    If Me!cmboyearfilter > "" Then _
    strFilter = strFilter & _
    " AND ([PreCal Date] BETWEEN #1/1/yyyy# AND #12/31/yyyy#=" & _
                         Format(CDate(Me!cmboyearfilter), _
                                "\#mm/dd/yyyy\#") & ")"
    
    
    
    If strFilter > "" Then strFilter = Mid(strFilter, 6)
         If strFilter <> strOldFilter Then
             Me.Filter = strFilter
             Me.FilterOn = (strFilter > "")
         End If
     End Sub
    Thanks for any help
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    I think this is what you are looking for:
    Code:
    strFilter = strFilter & _
     " AND (YEAR([PreCal Date])=" &  Me!cmboyearfilter & ")"

    Comment

    • beaker213
      New Member
      • Jul 2015
      • 2

      #3
      Well that stopped the runtime error but no records are returned no matter what date is entered. Maybe I have to do something to make the [cmboyearfilter] formatted to a year as well?

      I used the same logic as above and I think I got it. Thanks.

      Comment

      • jforbes
        Recognized Expert Top Contributor
        • Aug 2014
        • 1107

        #4
        I have to admit that I didn't read through all of the code. I just saw the point where the SQL was being created and so I assumed the that Me!cmboyearfilt er would be just an integer to represent the Year. Glad it all worked out for you.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          perhaps this will guide you, slightly different application with the grouping instead of with a cbobx; however, note how the SQL is written to return month and year from the data...

          you should be able to modify the concept to work as the recordsource for your cbobx
          Last edited by zmbd; Jul 12 '15, 12:07 AM.

          Comment

          Working...