Results 1 to 12 of 12
  1. #1
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283

    searching for records but keeping filter on the form

    I have a form that is filtered to only show the records aligned to the user when they log in. ON that same form I have a search box with the following code I found


    Code:
    Private Sub CmdFilter_Click()
    Dim strWhere As String
        Dim lngLen As Long
        Const conJetDate = "\#mm\/dd\/yyyy\#"
        If Not IsNull(Me.AccountName) Then
            strWhere = strWhere & "([Account] Like ""*" & Me.AccountName & "*"") AND "
        End If
       
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then 
            MsgBox "No criteria entered", vbInformation, "Null Search."
        Else 
            strWhere = Left$(strWhere, lngLen)
            Debug.Print strWhere
            Me.Filter = strWhere
            Me.FilterOn = True
        End If
    End Sub
    The issue is when I execute the search it returns all the records that match and not just the ones that belong to the logged in user. I am assuming I need to add a second criteria to the strWhere to limit the results to the current user to the following second of the search code
    Code:
    strWhere = strWhere & "([Account] Like ""*" & Me.AccountName & "*"") AND "
    but I have been unable to get it to work.

    The main form is filtered by looking at the value in the login form
    Code:
    Private Sub Form_Open(Cancel As Integer)
    Me.Filter = "[XeroxCanada_EmpNo] = '" & Forms!Logon_Form!cboUser.Column(0) & "'"""
     RunCommand acCmdApplyFilterSort
    End Sub
    Any help would be appreciated as always.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Try

    Code:
    strWhere = strWhere & "[Account] Like '*" & Me.AccountName & "*' AND [XeroxCanada_EmpNo] = '" & Forms!Logon_Form!cboUser.Column(0) & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    I get RunTime error 13 Type Mismatch. They both are Short Text as far as I can tell.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Use this and see what strWhere ends up being:

    http://www.baldyweb.com/ImmediateWindow.htm

    can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    I attached it. The code is on the onclick event of the Filter button at the top of frm_main

    Thanks for looking into this for me
    Attached Files Attached Files

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    It's annoyingly hard to test when you hide the Access window, but this:

    strWhere = strWhere & "[Account] Like '*" & Me.AccountName & "*' AND [XeroxCanada_EmpNo] = '" & Forms!Logon_Form!cboUser.Column(0) & "'" And ""

    should be

    strWhere = strWhere & "[Account] Like '*" & Me.AccountName & "*' AND [XeroxCanada_EmpNo] = '" & Forms!Logon_Form!cboUser.Column(0) & "' AND "

    Note the change at the end.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    sorry about that. When I try to remove the last " it automatically puts it back everytime

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Did you notice the others? Copy/paste that line and try it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Sorry I missed that one. It works as expected now.

    Thank you very much for the help

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    sorry but I forgot the undo step.

    When they hit the remove filter it shows all records instead of their records only.

    I added this to the code but it returns 0 records

    [CODE][/Private Sub cmdReset_Click()
    'Purpose: Clear all the search boxes in the Form Header, and show all records again.
    Dim ctl As Control
    Dim strWhere As String

    '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 = strWhere = strWhere & "[Account] Like '*" & Me.AccountName & "*' AND [XeroxCanada_EmpNo] = '" & Forms!Logon_Form!cboUser.Column(0) & "'"

    End Sub
    CODE]

  12. #12
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    sorry I figured it out fat fingered =strWhere

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

Similar Threads

  1. Replies: 1
    Last Post: 07-19-2016, 11:55 AM
  2. Searching through records on a form
    By Wagon in forum Forms
    Replies: 2
    Last Post: 04-22-2016, 07:14 AM
  3. Replies: 6
    Last Post: 07-27-2015, 10:23 AM
  4. Replies: 12
    Last Post: 04-07-2012, 12:09 PM
  5. Replies: 10
    Last Post: 01-10-2011, 07:52 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