Results 1 to 15 of 15
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Suggestions on how to fix search form error?

    I have a form that I'm using to filter a report. The user inputs the date range and then can type in a category, and the form will filter a report.

    The VBA for the short text field is below and I can't seem to code it correct. I'm getting a 3075 error on the sFilter line. Any suggestions?

    Code:
        If Not IsNull(Me.txtComplaintCategory) Then
                      sFilter & "[ComplaintCategory] ='" & Me.txtComplaintCategory & "'"
        End If
    I can provide more code if needed.

  2. #2
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    232
    Switch the & and =
    Groeten,

    Peter

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    You may also need an ‘ and ‘ before [complaintcategory]

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I agree with the other responses. Also, if you had ComplaintCategory in a combo, then you might eliminate some spelling or typos if that is an issue.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Then again you could just Debug.Print sFilter to see the error?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by xps35 View Post
    Switch the & and =
    Which '&' exactly?

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
     sFilter = "[ComplaintCategory] = """ & Me.txtComplaintCategory & """"
    But I dont think thats right. Aren't you also filtering by a date range also?

    I would expect something like (aircode)

    Code:
    Dim strFilter as string
    
    strFilter = "MyDateRangeField between #" & startDate & "# and # " & endDate & "#"
    
    if not isnull(Me.txtComplaintCategory ) then
    strFilter = strFilter & " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """"
    end if
    
    me.filter = strfilter
    me.filteron = true
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    My thoughts are that the category needs to be added to the existing filter (which is date criteria?)
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    232
    Quote Originally Posted by templeowls View Post
    Which '&' exactly?
    Sorry, the first.
    Groeten,

    Peter

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Why switch? Not this??
    Code:
    sFilter = "[ComplaintCategory] ='" & Me.txtComplaintCategory & "'"
    I don't see any evidence of trying to incorporate a date in the posted filter so I left that alone. I've grown tired of posts with only error numbers so if that one has something to do with a date then ignore me.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Quote Originally Posted by templeowls View Post
    The user inputs the date range and then can type in a category, and the form will filter a report.
    OP is building a multi-part criteria string. I don't see anything wrong with posted code. Possibly sFilter is lacking AND operator. Without previous code, can't really know. Review http://allenbrowne.com/ser-62.html

    Agree, complaints should be selected from combobox unless want to do pattern matching in which case need LIKE and wildcards instead of =. If there is a standardized set of complaints then should save a ComplaintID into data and use that value in search.

    Should provide exact error message, not just error number.
    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.

  12. #12
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by June7 View Post
    OP is building a multi-part criteria string. I don't see anything wrong with posted code. Possibly strFilter is lacking AND operator. Without previous code, can't really know. Review http://allenbrowne.com/ser-62.html

    Agree, complaints should be selected from combobox unless want to do pattern matching in which case need LIKE and wildcards instead of =. If there is a standardized set of complaints then should save a ComplaintID into data and use that value in search.

    Should provide exact error message, not just error number.
    Sorry everyone. Here's the full code.

    Code:
    Private Sub btnSearch_Click()
    
        Dim frm As Form
        Dim sFilter As String
        
        Set frm = [Forms]![frm_complaints]
        
        If Len(sFilter) <> 0 Then
            sFilter = "[ComplaintNumber] IN (" & sFilter & ") AND "
        End If
        
        If Not IsNull(Me.txtComplaintCategory) Then
            sFilter = sFilter & " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """"
        End If
        
        If IsDate(Me.txtStartDate) Then
            sFilter = sFilter & "[ComplaintDate] >= #" & Format$(Me.txtStartDate, "YYYY-MM-DD") & "# AND "
        End If
        
        If IsDate(Me.txtEndDate) Then
            sFilter = sFilter & "[ComplaintDate] <= #" & Format$(Me.txtEndDate, "YYYY-MM-DD") & "# AND "
        End If
        
        If Len(sFilter) <> 0 Then
            sFilter = Left$(sFilter, Len(sFilter) - 5)
            With frm
                .Filter = sFilter
                .FilterOn = True
            End With
        Else
            frm.FilterOn = False
        End If
        Set frm = Nothing
    End Sub
    The date range code works flawlessly to filter down the form (apologies, I mistakenly said this is to filter a report earlier), however the txtComplaintCategory doesn't work.

    I get a 3075 error that states: syntax error (missing operator) in query expression 'and [ComplaintCategory ="' and when I debug it, it highlights the ".filter = sFilter" line

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Dim sFilter As String

    Set frm = [Forms]![frm_complaints]

    If Len(sFilter) <> 0 Then << of course it does. Nothing has been assigned to it.

    so sFilter = "" thus IF IsNull...
    sFilter = sFilter & " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """"
    translates to "" & " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """"

    so sFilter now = " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """"

    IF IsNUll was true AND Me.txtStartDate is a date then concatenate this
    " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """" onto what you already have so then it becomes

    [ComplaintNumber] IN (" & sFilter & ") "and [ComplaintCategory] = """ & Me.txtComplaintCategory & """

    but since sFilter was " and [ComplaintCategory] = """ & Me.txtComplaintCategory doesn't it become

    [ComplaintNumber] IN (" & " and [ComplaintCategory] = """ & Me.txtComplaintCategory " and [ComplaintCategory] = """ & Me.txtComplaintCategory & """"

    I dunno - my eyes are starting to spin. Maybe I'm full of it because only one of those IF's could ever be, but it sure doesn't look that way.

    Surely you have been told walk through your code and check your variables at least once before?? If not, then I will be the first I guess.


    If Len(sFilter) <> 0 Then
    Debug.Print sFilter
    Stop
    Last edited by Micron; 12-06-2022 at 08:07 PM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    You are missing & " AND " at the end of ComplaintCategory criteria string. I should have recognized that from initial post but seeing rest of code does help.

    What are the complaint categories? Do you have a lookup table for them? Why not save CategoryID instead of descriptive text and use a combobox instead of textbox? Can a complaint be associated with multiple categories?

    Could use same code to build filter criteria for a report.
    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.

  15. #15
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Also the ' and ' at the start of that line should not be there (needs to be at the end as June says) - false assumption on my part that it would only be added if that particular criteria was required.

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

Similar Threads

  1. Search Form Error
    By N7925Y in forum Programming
    Replies: 3
    Last Post: 10-21-2016, 10:25 AM
  2. Replies: 5
    Last Post: 07-25-2014, 02:57 PM
  3. Code for search form error
    By jfn15 in forum Forms
    Replies: 4
    Last Post: 06-11-2013, 09:02 AM
  4. Suggestions for Form Design
    By KrisDdb in forum Forms
    Replies: 2
    Last Post: 12-08-2011, 02:31 PM
  5. Search engine result database suggestions
    By PIHA520 in forum Access
    Replies: 0
    Last Post: 05-29-2011, 05:44 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