Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9

    Multi-Search Form including StartDate and EndDate - dates not working with keyword

    Hi,


    I am trying to create a search form that creates a list of desired results. My date fields work when I only use both of them. The text fields work (including the combo box) when I use them - but when I use the text (keyword search) AND the "between StartDate AND EndDate", it's like that portion of the search string is ignored. And when I only type in a StartDate, no results are returned. I know I am missing some code - can you tell me that might be?

    This is the code I am using (I found 2 online & tweaked - I can provide the source if needed):

    Code:
    Dim strWhere As String
    Dim lngLen As Long
    
    If Not IsNull(Me.Keyword_Search) Then
            strWhere = strWhere & "([Description] Like ""*" & Me.Keyword_Search & "*"") OR "
        End If
        
       If Not IsNull(Me.Keyword_Search) Then
            strWhere = strWhere & "([Title] Like ""*" & Me.Keyword_Search & "*"") OR "
        End If
        
        If Not IsNull(Me.Keyword_Search) Then
            strWhere = strWhere & "([Subject] Like ""*" & Me.Keyword_Search & "*"") AND "
        End If
    
    If Not IsNull(Me.CmbFormat) Then
            strWhere = strWhere & "([PhysicalFormat] = """ & Me.CmbFormat & """) AND "
        End If
    
    If Not IsNull(Me.StartDate) Then
              strWhere = strWhere & "([OriginalDate] between #" & (Me.StartDate) & "# AND #" & (Me.EndDate) & "#) AND "
           Else
           strWhere = strWhere & "([OriginalDate] >= #" & Me.StartDate & " #"
           strWhere = strWhere & "([OriginalDate] <= #" & Me.EndDate & " #"
        End If
    
    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then
    
    Else
    strWhere = Left$(strWhere, lngLen)
    
    Debug.Print strWhere
    
    Me.Filter = strWhere
            Me.FilterOn = True
        End If
    End Sub
    
    Private Sub cmdReset_Click()
            
         'Clear all the controls in the Form Header section.
        For Each ctl In Me.Section(acHeader).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox
                ctl.Value = Null
            Case acCheckBox
                ctl.Value = False
            End Select
        Next
        
        'Remove the form's filter.
        Me.Filter = "(False)"
     
    End Sub
    
    Private Sub Form_BeforeInsert(Cancel As Integer)
       Cancel = True
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
        Me.Filter = "(False)"
        Me.FilterOn = True
    End Sub
    Thanks for any help!
    Barbara

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Try:

    If Not IsNull(Me.StartDate) AND Not IsNull(Me.EndDate) Then
    ...
    ElseIf Not IsNull(Me.StartDate) Then
    ...
    ElseIf Not IsNull(Me.EndDate) Then
    ...
    End If

    Don't forget the " AND " at the end of each of the date criteria. You left off of the alternate date statements. The Left truncation will fail.
    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.

  3. #3
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9
    Quote Originally Posted by June7 View Post
    Try:

    If Not IsNull(Me.StartDate) AND Not IsNull(Me.EndDate) Then
    ...
    ElseIf Not IsNull(Me.StartDate) Then
    ...
    ElseIf Not IsNull(Me.EndDate) Then
    ...
    End If

    Don't forget the " AND " at the end of each of the date criteria. You left off of the alternate date statements. The Left truncation will fail.
    ________________

    Thank you!!!

    However, I have to admit that I am not really versed in code...Not sure what the alternate date statements should be, nor which "AND" I need to add. Also - do your 3 dots indicate other text that should be included?

    Thank you again for your time & quick response.
    Barbara

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    The 3 dots mean enter the necessary code.

    You are missing " AND " at the ends of:
    strWhere = strWhere & "([OriginalDate] >= #" & Me.StartDate & " #"
    strWhere = strWhere & "([OriginalDate] <= #" & Me.EndDate & " #"

    The alternative date statements would be each of the above lines.
    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.

  5. #5
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9
    Ok,
    This is what that part now looks like:

    If Not IsNull(Me.StartDate) And Not IsNull(Me.EndDate) Then


    strWhere = strWhere & "([OriginalDate] between #" & (Me.StartDate) & "# AND #" & (Me.EndDate) & "#) AND "

    ElseIf Not IsNull(Me.StartDate) Then
    ElseIf Not IsNull(Me.EndDate) Then

    Else
    strWhere = strWhere & "([OriginalDate] >= #" & Me.StartDate & " #" And ""
    strWhere = strWhere & "([OriginalDate] <= #" & Me.EndDate & " #" And ""

    End If


    Is that what it should look like? Am I missing some other code or did I put your suggestions in the wrong place? Because it still works the same way it did before.

    Thanks for trying to help!
    Barbara

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    No, put the StartDate line under the StartDate ElseIf and the EndDate line under the EndDate ElseIf.

    Remove the 'Else'.

    Too many quote marks

    ElseIf Not IsNull(Me.StartDate) Then
    strWhere = strWhere & "([OriginalDate] >= #" & Me.StartDate & "# AND "
    ElseIf Not IsNull(Me.EndDate) Then
    strWhere = strWhere & "([OriginalDate] <= #" & Me.EndDate & "# AND "
    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.

  7. #7
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9
    Ok. Thank you for your reply!!

    Sorry to say, it still does not pull dates "between" when including keywords in the search string. It seems to be ignoring that part of the string request. Like when a parameter query has too many parameters.

    This is what I changed based on what I understood you to say:

    If Not IsNull(Me.StartDate) And Not IsNull(Me.EndDate) Then
    strWhere = strWhere & "([OriginalDate] between #" & (Me.StartDate) & "# AND #" & (Me.EndDate) & "#) AND "

    ElseIf Not IsNull(Me.StartDate) Then
    strWhere = strWhere & "([OriginalDate] >= #" & Me.StartDate & " # AND "

    ElseIf Not IsNull(Me.EndDate) Then
    strWhere = strWhere & "([OriginalDate] <= #" & Me.EndDate & " # AND "


    End If

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    A parameterized query can have too many parameters?

    Seems to me that should work. I do notice error in the alternate date criteria - there is a solo paren in each line, remove it or add its mate.

    At this point, I would have to review db to debug. If you want to provide, follow instructions at bottom of my post.

    Also, refer to link at bottom of my post for guidance on debugging techniques.
    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.

  9. #9
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9
    Thank you very much for helping me get the code cleaned up! Unfortunately, I still do not get the results I am looking for. This is what the query code looks like once it's run:

    ([Description] Like "*blue*") OR ([Title] Like "*blue*") OR ([Subject] Like "*blue*") AND ([PhysicalFormat] = "10") AND ([OriginalDate] between #3/1/2000# AND #4/1/2000#) but the dates returned are all dates in the db, not the just dates between 3/1/00 and 4/1/00 with the keyword "blue" (which should be one record).

    I will try to send db.

    Thank you again!
    Barbara Archives - Copy.zip
    Last edited by BarbaraChurchill; 01-29-2013 at 03:02 PM. Reason: Adding Database

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    I did not notice the OR criteria before. This really complicates. Combining OR and AND operators is tricky.

    Try placing the 3 OR criteria enclosed together in parentheses.

    ([Description] Like "*blue*" OR [Title] Like "*blue*" OR [Subject] Like "*blue*") AND

    Also, the Left trim will fail if strWhere ends in " OR ".
    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.

  11. #11
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9
    That doesn't work either. I have been working with the "On Open" property of the SEARCH form.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    You are setting the Filter and FilterOn properties in Open event. The filter code is in button Click event. This all looks good. Have you step debugged? Because of FindingGuide table I can't run the code until I can use Access 2010 tonight.
    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.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Finally hit me the OR criteria are all from the same control. Don't need separate If Thens. Try:
    Code:
        If Not IsNull(Me.Keyword_Search) Then
            strWhere = strWhere & "([Description] Like '*" & Me.Keyword_Search & "*' OR " & _
            "[Title] Like '*" & Me.Keyword_Search & "*' OR " & _
            "[Subject] Like '*" & Me.Keyword_Search & "*') AND "
        End If
        If Not IsNull(Me.CmbFormat) Then
            strWhere = strWhere & "([PhysicalFormat] = '" & Me.CmbFormat & "') AND "
        End If
        If Not IsNull(Me.StartDate) And Not IsNull(Me.EndDate) Then
            strWhere = strWhere & "([OriginalDate] between #" & (Me.StartDate) & "# AND #" & (Me.EndDate) & "#) AND "
        ElseIf Not IsNull(Me.StartDate) Then
            strWhere = strWhere & "([OriginalDate] >= #" & Me.StartDate & " #) AND "
        ElseIf Not IsNull(Me.EndDate) Then
             strWhere = strWhere & "([OriginalDate] <= #" & Me.EndDate & " #) AND "
        End If
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then     'Nah: there was nothing in the string.
           'MsgBox "No criteria", vbInformation, "Nothing to do."
        Else                    'Yep: there is something there, so remove the " AND " at the end.
            strWhere = Left$(strWhere, lngLen)
            Me.Filter = strWhere
            Me.FilterOn = True
        End If
    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.

  14. #14
    BarbaraChurchill is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    9
    Thank you so much for all of your time!! Unfortunately, that doesn't work either. What I noticed is that the Physical format by itself doesn't filter correctly either and with your suggested format, doesn't work at all. So - I'm really stumped!

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    I tested Keyword and date range and that worked. I will test some more tonight.
    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.

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

Similar Threads

  1. A TRUE Keyword Search
    By Meep in forum Queries
    Replies: 72
    Last Post: 05-13-2013, 06:45 PM
  2. Multiple Keyword Search
    By gatsby in forum Access
    Replies: 15
    Last Post: 01-21-2013, 10:53 PM
  3. Multi-Field Search Query not working
    By omair1051992 in forum Queries
    Replies: 16
    Last Post: 06-19-2012, 05:46 AM
  4. Replies: 5
    Last Post: 06-17-2011, 11:35 AM
  5. Making a keyword search
    By timmy in forum Forms
    Replies: 9
    Last Post: 03-14-2011, 02:57 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