Results 1 to 3 of 3
  1. #1
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33

    filter record with date

    Hello Access experts!
    I have a textbox where I put the date dd/mm/yyyy and a filter button that filter the records as per textbox. If in the textbox there is not the entire date the result is 0 record.

    I would like that the user can simply fill the textbox with only year yyyy, or month and year mm/yyyy, and the result are the records with these fields in the date.

    I tried this and works with the entire date written:



    Code:
    Private Sub Comando157_Click()
    
    
    If IsNull(Me.Testo155) Then
            Me.FilterOn = False
        Else
            Me.Filter = "DATA Like '" & Me.Testo155.Value & "*" & "'"
            Me.FilterOn = True
        End If
    
    
    End Sub
    Where Testo155 is the textbox where I write the date and DATA the field date of the record.

    I do not know how to do the control for only year and month/year.

    Please, do you have any idea?

    Beppe

  2. #2
    Join Date
    Apr 2017
    Posts
    1,679
    Code:
    If Len(Me.Testo155) = 2 Then
    Me.Filter = "YEAR(Data) = " & CStr(2000 + CInt(Me.Testo155))
    ElseIf Len(Me.Testo155) = 4 Then
    Me.Filter = "YEAR(Data) = " & CStr(Me.Testo155)
    ElseIf Len(Me.Testo155) = 6 Then // Use format yyyymm for year and month, as otherwise you'll have a text in combo
    Me.Filter = "YEAR(Data) = " & Left(CStr(Me.Testo155),4) & " AND MONTH(Data) = " Right(CStr(Me.Testo155),2)
    ElseIf Len(Me.Testo155) = 10 Then
    Me.Filter = "Data = " & CInt(Me.Testo155) // In case Data is date, not Datetime. Or place date text between # characters.
    Else
    Me.FilterOn = False

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    DATA is a text type field?

    Try using second wildcard:

    Me.Filter = "DATA Like '*" & Me.Testo155.Value & "*'"

    However, using a single input control depends on user correctly entering date structured with /'s. Better to input month and year in separate controls (maybe comboboxes) and use code to dynamically construct filter criteria.

    Review http://allenbrowne.com/ser-62.html
    Last edited by June7; 09-26-2017 at 01:14 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 4
    Last Post: 11-20-2018, 11:57 PM
  2. Replies: 6
    Last Post: 05-26-2017, 04:10 PM
  3. Replies: 11
    Last Post: 06-28-2015, 06:42 PM
  4. Replies: 6
    Last Post: 07-21-2013, 10:43 PM
  5. Replies: 14
    Last Post: 05-25-2012, 02:40 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