Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828

    ListBox with search criteria?

    Click image for larger version. 

Name:	Form1.jpg 
Views:	41 
Size:	34.0 KB 
ID:	46834

    Hi all,
    I have a unbound form with an unbound listbox on it that is populated from a union qry.
    Below is the folling code i am trying to use to get it to search if I enter something in the above txt boxes.


    It requeries when i enter like R in the firstname but I still get all records showing?
    Its just sample data and i just cannot for the life of me figure out why this isnt populating the list box with the search
    Criteria? Also on after update on all the filter txt boxes i have =FilterPerson() so this should trigger the filter click
    This should work. I have it in one of my old db's and it works just fine.... Not sure what I am doing wrong.
    Please do assist....Thanks
    Dave

    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub cmdReset_Click()
        'Purpose:   Clear all the search boxes in the Form , and show all records again.
        Dim ctl As Control
        
        'Clear all the controls in the Form  section.
        For Each ctl In Me.Section(acHeader).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox, acOptionGroup
                ctl = Null
            Case acCheckBox
                ctl.Value = False
            End Select
        Next
        
        'Remove the form's filter.
        Me.FilterOn = False
        FilterPerson
    End Sub
    
    
    Private Sub cmdFilter_Click()
        Dim strWhere As String
        Dim lngLen As Long
        Dim s As String
        '***********************************************************************
        'Chop off the trailing " AND ", and use the string as the form's Filter.
        '***********************************************************************
        'See if the string has more than 5 characters (a trailng " AND ") to remove.
        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)
            'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
            Debug.Print strWhere
            
            'Finally, apply the string as the form's Filter.
            Me.Filter = strWhere
            Me.FilterOn = True
        End If
        
    End Sub
    
    
    Private Sub Form_Load()
    'not bound -- no data is loaded
       cmdReset_Click
    End Sub
    
    
    Private Function FilterPerson()
       'this is the main procedure for filtering
        Dim strWhere As String
        Dim s As String
        
        s = "SELECT PersonID, FirstName, LastName, Association, IsActive From qryPersonSearch;"
        
       If Not IsNull(Me.FilterFirstName) Then
          strWhere = strWhere & "([FirstName] LIKE ""*" & Me.FilterFirstName & "*"") And "
       End If
    
    
       If Not IsNull(Me.FilterLastName) Then
          strWhere = strWhere & "([LastName] LIKE ""*" & Me.FilterLastName & "*"") And "
       End If
    
    
       If Not IsNull(Me.FilterAssociation) Then
          strWhere = strWhere & "([Association] LIKE ""*" & Me.FilterAssociation & "*"") And "
       End If
       
        Debug.Print s
    
    
       
        Me.LstPersonSearch.RowSource = s
        Me.LstPersonSearch.Requery
        
    End Function

    I have tried this code also but no difference in results?

    Code:
    Private Function FilterPerson()
       'this is the main procedure for filtering
       
       Dim s As String, mWhere As String
    	s = "SELECT PersonID, FirstName, LastName, Association, IsActive From qryPersonSearch;"
          
        
       If Not IsNull(Me.FilterFirstName) Then
          mWhere = (mWhere & IIf(Len(mWhere) > 0, " AND ", "")) & "FirstName LIKE '*" & Me.FilterFirstName & "*'"
       End If
    
    
       If Not IsNull(Me.FilterLastName) Then
          mWhere = (mWhere & IIf(Len(mWhere) > 0, " AND ", "")) & "LastName LIKE '*" & Me.FilterLastName & "*'"
       End If
    
    
       If Not IsNull(Me.FilterAssociation) Then
          mWhere = (mWhere & IIf(Len(mWhere) > 0, " AND ", "")) & "Association LIKE '*" & Me.FilterAssociation & "*'"
       End If
       
    Debug.Print s
    
    
        Me.LstPersonSearch.RowSource = s
        Me.LstPersonSearch.Requery
        
    End Function
    
    
    ...........................................................
    
    
    Private Sub Form_Load()
    'not bound -- no data is loaded
       cmdReset_Click
    End Sub
    
    
    ............................................................
    
    
    Private Sub cmdFilter_Click()
        
        '***********************************************************************
        'Chop off the trailing " AND ", and use the string as the form's Filter.
        '***********************************************************************
        'See if the string has more than 5 characters (a trailng " AND ") to remove.
        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)
            'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
            'Debug.Print strWhere
            
            'Finally, apply the string as the form's Filter.
            Me.Filter = strWhere
            Me.FilterOn = True
        End If
        
    End Sub
    
    
    .......................................................................
    
    
    Private Sub cmdReset_Click()
        'Purpose:   Clear all the search boxes in the Form , and show all records again.
        Dim ctl As Control
        
        'Clear all the controls in the Form  section.
        For Each ctl In Me.Section(acHeader).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox, acOptionGroup
                ctl = Null
            Case acCheckBox
                ctl.Value = False
            End Select
        Next
        'Me.grpCompanyFilter = 27
        
        'Remove the form's filter.
        Me.FilterOn = False
        FilterPerson
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    That's a lot of code to read through. I would rather have db and debug.

    Speaking of debugging, have you? Set breakpoint and step through code.
    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
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Tempdb.zip

    Here is a temp db to work with
    Thanks
    Dave
    Last edited by d9pierce1; 12-06-2021 at 12:49 AM. Reason: upload db

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You have no WHERE ? in the last code shown

    The debug.print would show that?

    A filter statement does not use the WHERE, but SQL does?
    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

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Need to concatenate s and strWhere variables but remove the semi-colon.

    s = "SELECT PersonID, FirstName, LastName, Association, IsActive FROM qryPersonSearch "

    s = s & strWhere

    Debug.Print s

    Need an AND operator between each criteria. For example of handling that, review http://allenbrowne.com/ser-62.html
    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.

  6. #6
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You might review the design of the table table "tblPerson"; I would say it is not normalized because you have repeating fields (which is why you have to use a Union query for the List box Row Source.

    Firstname sFirstName
    MiddleName sMiddleName
    LastName sLastName
    UseMaiden sUseMaiden
    UseMaidenLast sUseMaidenLast
    MaidenName sMaidenName



    And what about the fields "Created" and "Modified"? What part of the record is Modified? For the FirstName group (6 fields) or the sFirstname group (6 fields)?


    At first I wasn't sure what you wanted to search - the form or the listbox. I guessed the list box (because the form is unbound ) Duh!!

    So see if this is what you were trying to do..... ( the filter button has no code because the form is unbound - the code didn't do anything....)
    Attached Files Attached Files

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Hi ssanfu,
    I do like those changes you made! Very nice indeed.
    I have two questions if you dont mind,

    1. If I were to add the checkbox "IsActive" to the filter, how would I specify that in the code? I cant find an example of that.
    How would I wrght the code to use a check box in the filter section?

    Such as like the following except a check box!

    Code:
       'check FilterFirstName   'FilterIsActive to replace FilterFirstName
        Forms!form1.FilterFirstName.SetFocus
        If Len(Forms!form1.FilterFirstName.Text) & "" > 0 Then
          strWhere = strWhere & "([FirstName] LIKE ""*" & Forms!form1.FilterFirstName.Text & "*"") And "
        End If
    2. If I were to have a search form with say two check boxes and I wanted one to be true, and one to be false on reset, is that possible in the Case portion of reset code?
    I know I dont have two check boxes in this form but i do in another which i would like to know if possible to keep one True, and one False ?

    Code:
    Private Sub cmdReset_Click()
        'Purpose:   Clear all the search boxes in the Form , and show all records again.
        Dim ctl As Control
        
        'Clear all the controls in the Form Header section.
        For Each ctl In Me.Section(acHeader).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox, acOptionGroup
                ctl = Null
            Case acCheckBox
                ctl.Value = False  'IsActive=True, IsPreferred=False (I would assume it would go like? ctl.IsActive.Value = True and ctl.IsPreferred.Value = False)
            End Select
        Next
        
        'Remove the form's filter.
        Me.FilterOn = False
        FilterPerson
    End Sub

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Click image for larger version. 

Name:	frmPerson.jpg 
Views:	29 
Size:	107.0 KB 
ID:	46865

    ssanfu,
    This is the form for that person table and may explain the Maiden and such...
    The Created and Modified are the time stamp file was created and if modified, the time stamp for that in before update

  9. #9
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by d9pierce1 View Post
    This is the form for that person table and may explain the Maiden and such...
    The Created and Modified are the time stamp file was created and if modified, the time stamp for that in before update
    I might have designed this a little different, but......if it works for you .

    As far as the 2nd boolean field, I think I got it. Had to change a few things. Maybe a little more than a few.

    I renamed the function in Module1 (added an "f" prefix )and added "Call" ( 2 places in the "Change event code)

    Play with it ........ tear it apart... maybe it will help (or maybe not...)

    In the list box, the boolean fields were displayed as 0/-1. It took a while, but I finally got 'No'/'Yes' to display *AND* was still able to get the filtering to work!!
    Attached Files Attached Files

  10. #10
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Hi ssanfu,
    Thank you so much, I have tested it out and appriciate you help... These search things are a royal pain to me.
    I only found one issue and it is where if you uncheck IsActive in table for record 2, then it wont filter the form to show the not active persons....?
    I will play with it as the IsPreferred works just wonderful... Not sure why the IsActive wont show files that are non active?
    But in any event, thank you so much
    Dave

  11. #11
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I missed that.

    Stupid computer does what I tell it to do, not what I want it to do!

    OK, try changing the following section of code in "Function fListBoxFilter()" to
    Code:
    
       'check chkIsActive
        If Forms!form1.chkIsActive = True Then
          strWhere = strWhere & "([Active] = 'Yes') And "
        End If
    
       'check IsPreferred
        If Forms!form1.chkIsPreferred = True Then
          strWhere = strWhere & "([Preferred] = 'Yes') And "
        End If
    
        'chop off the last 5 characters
        lngLen = Len(strWhere) - 5
        If lngLen > 0 Then    '<<-- if  Len of strWHERE is > 0, then there must be a filter to apply
            strWhere = Left(strWhere, lngLen)
            strWhere = " WHERE " & strWhere  ' add "WHERE to the filter string (strWHERE)
        End If
        
    '    Debug.Print Len(strWhere)
    '    Debug.Print s
    '    Debug.Print s & strWhere
        
        Forms!form1.LstPersonSearch.RowSource = s & strWhere
    '    Me.LstPersonSearch.Requery
        
    End Function

  12. #12
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Thank you ssanfu
    That is just awsome!
    I would have never figured that out on my own....
    Again, Thank you and will mark as solved!
    Dave

  13. #13
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    TempTestNew.zip

    Hi all,
    I had this marked as solved but changed that as I ran into an issue with a cbo in the search function.
    ssanfu was kind enough to create this for me and worked like a charm...
    I am using it as a template for other search criteria and i cannot figure out the Cbo... Questionalble code is in red below
    I am sure it is something with the way i have wwritten the criteria for that but have tried many mehods and nothing has success yet....
    I didnt have a cbo in the origional so trying to figure out how to add the cbo filter code.
    Please assist me if you would be so kind

    Code:
    Option Compare Database
    Option Explicit
    
    
    'row source for listbox
    Public Const s As String = "SELECT PersonID, FirstName, LastName, Association, Active From qryPersonSearch"
    
    
    Function fListBoxFilter()
       'this is the main procedure for filtering
        Dim strWhere As String
    '    Dim s As String
        Dim lngLen As Long
       
       'check FilterFirstName
        Forms!form1.FilterFirstName.SetFocus
        If Len(Forms!form1.FilterFirstName.Text) & "" > 0 Then
          strWhere = strWhere & "([FirstName] LIKE ""*" & Forms!form1.FilterFirstName.Text & "*"") And "
        End If
      
       'check FilterLastName
        Forms!form1.FilterLastName.SetFocus
        If Len(Forms!form1.FilterLastName.Text) & "" > 0 Then
          strWhere = strWhere & "([LastName] LIKE ""*" & Forms!form1.FilterLastName.Text & "*"") And "
        End If
    
    
       'check cboFilterAssociation
        Forms!form1.cboFilterAssociation.SetFocus
        If Len(Forms!form1.cboFilterAssociation.Value) & "" > 0 Then
          strWhere = strWhere & "([Association] = Forms!form1.cboFilterAssociation.Value) And "
        End If
    
    
       'check chkIsActive
        If Forms!form1.chkIsActive = True Then
          strWhere = strWhere & "([Active] = 'Yes') And "
        End If
    
    
        'chop off the last 5 characters
        lngLen = Len(strWhere) - 5
        If lngLen > 0 Then    '<<-- if  Len of strWHERE is > 0, then there must be a filter to apply
            strWhere = Left(strWhere, lngLen)
            strWhere = " WHERE " & strWhere  ' add "WHERE to the filter string (strWHERE)
        End If
        
    '    Debug.Print Len(strWhere)
    '    Debug.Print s
    '    Debug.Print s & strWhere
        
        Forms!form1.LstPersonSearch.RowSource = s & strWhere
    '    Me.LstPersonSearch.Requery
        
    End Function

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You do not say what the error/problem is ????

    Likely mismatch?
    If the criteria is text, then it needed to be surrounded by single quotes for the most situations, or triple quotes as you do for the other criteria?

    I am sure you have had to do this before?
    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

  15. #15
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Hi,
    The problem is if I select from cbo it doesnt filter the list box, just empties it, sorry i didnt state that....
    I really have not done this in this format. This function he wrote is totally new to me I will say.
    I was able to get it correct in the format i used prior but not this one.
    I have tried .value, "", "'", and so many ways. It complies but does not filter...
    Have no real idea on how to write that line to make it filter...
    Thanks

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

Similar Threads

  1. Sort/Fitler listbox or Search Listbox
    By Behedwin in forum Access
    Replies: 5
    Last Post: 11-09-2017, 12:27 PM
  2. Replies: 3
    Last Post: 09-12-2016, 11:49 AM
  3. Replies: 1
    Last Post: 03-26-2015, 11:08 AM
  4. Search Form - Dialog box asks for search criteria
    By faythe1215 in forum Queries
    Replies: 3
    Last Post: 02-10-2015, 05:54 PM
  5. Replies: 3
    Last Post: 01-04-2015, 06:09 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