Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14

    Search form with list boxes

    G'Day all,



    I have a form that I use as a search form. All is well right now, but I would like to change a few of my combo boxes to list boxes so the user can select a number of different criteria instead of one. I've been googling and have found some solutions to this; however, because my list boxes are part of group of search criteria, I am having trouble implementing it. Below I've posted the code for my search button. Sorry, it's fairly long.

    The parts of the code I need help with are the ones related to [Type], [Category] and [Subcategory]. These are combo boxes now, but I'd like them to be List Boxes with multi select.

    Note: I know how to convert the boxes and set properties, I just need help with the code side.

    Code:
    Private Sub cmdFilter_Click()
        Dim strWhere As String
        Dim lngLen As Long
        Dim varItem As Variant
        Dim strDelim As String
        Dim i As Variant
        Dim Criteria As String
        Const conJetDate = "\#mm\/dd\/yyyy\#"
        
        If Not IsNull(Me.txtFilterDWG) Then
            strWhere = strWhere & "([Drawing #] Like ""*" & Me.txtFilterDWG & "*"") AND "
        End If
        
        If Not IsNull(Me.txtFilterRev) Then
            strWhere = strWhere & "([Rev] Like ""*" & Me.txtFilterRev & "*"") AND "
        End If
        
        If Me.cbFilterOBS = True Then
            strWhere = strWhere & "([Rev] <> 'OBS') AND "
        End If
        
        If Not IsNull(Me.txtFilterStartDate) Then
            strWhere = strWhere & "([Date] >= " & Format(Me.txtFilterStartDate, conJetDate) & ") AND "
        End If
        
        If Not IsNull(Me.txtFilterEndDate) Then
            strWhere = strWhere & "([Date] <= " & Format(Me.txtFilterEndDate, conJetDate) & ") AND "
        End If
        
        If Not IsNull(Me.txtFilterDescription) Then
            strWhere = strWhere & "([Description] Like ""*" & Me.txtFilterDescription & "*"") AND "
        End If
        
        If Not IsNull(Me.txtFilterType) Then
            strWhere = strWhere & "([Type] Like ""*" & Me.txtFilterType & "*"") AND "
        End If
        
        If Not IsNull(Me.txtFilterCategory) Then
            strWhere = strWhere & "([Category] Like ""*" & Me.txtFilterCategory & "*"") AND "
        End If
    
        If Not IsNull(Me.txtFilterSubcategory) Then
            strWhere = strWhere & "([Subcategory] Like ""*" & Me.txtFilterSubcategory & "*"") AND "
        End If
        
        If Me.cboFilterCad = -1 Then
            strWhere = strWhere & "([Cad DWG] = True) AND "
        ElseIf Me.cboFilterCad = 0 Then
            strWhere = strWhere & "([Cad DWG] = False) AND "
        End If
        
        If Not IsNull(Me.txtFilterComments) Then
            strWhere = strWhere & "([Comments] Like ""*" & Me.txtFilterComments & "*"") AND "
        End If
        
        If Not IsNull(Me.txtFilterSize) Then
            strWhere = strWhere & "([Size] = """ & Me.txtFilterSize & """) AND "
        End If
        
        If Not IsNull(Me.txtFilterPN) Then
            strWhere = strWhere & "([Part Number] Like ""*" & Me.txtFilterPN & "*"") AND "
        End If
        
        If Me.cboFilterLinked = -1 Then
            strWhere = strWhere & "(Not IsNull([DWG_Filename])) AND "
        ElseIf Me.cboFilterLinked = 0 Then
            strWhere = strWhere & "(IsNull([DWG_Filename])) AND "
        End If
        
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then
            MsgBox "Nothing to do"
        Else
            strWhere = Left$(strWhere, lngLen)
            Me.Filter = strWhere
            Me.FilterOn = True
        End If
            
    End Sub
    Thanks!
    Scott

  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,521
    Here's one method:

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

    You could adapt it to add to your filter string.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    Thanks for your reply

    That looks similar to what I've been trying to get to work before posting. Perhaps I need a little help getting it adapted...

    I've changed the [Category] line to this:

    Code:
        If Not IsNull(Me.txtFilterCategory) Then
            Set ctl = Me.txtFilterCategory
            For Each varItem In ctl.ItemsSelected
                strWhere = strWhere & ctl.ItemData(varItem) & ","
            Next varItem
        End If
    I'm getting: Run-time error: '2448': You can't assign a value to this object.

  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,521
    On what line? FYI, a multiselect listbox will always be Null, so your test will never be met. You need to test ItemsSelected.Count as I did in my example. That will also not result in a valid string, because you're not adding the "FieldName IN(...) part.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    I took out the first If statement.

    2448 is highlighting "Me.Filter = strWhere"

    Since I'm not opening a report, where should I put the FieldName IN? I'm just filtering a continuous form.

  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,521
    In my example, I'm building a string for the wherecondition argument of OpenReport. Both the wherecondition and filter are structured the same, as both are an SQL "WHERE" clause without the word "where". If you're getting an error there, I wonder if the string is valid. Add this line after the string is built but before it's used:

    Debug.Print strWhere

    That will print the finished string out to the VBA Immediate window. If you don't see the problem with it there, paste it here.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    Hmm, sorry for a noob question, but where should I put Debug.Print strWhere?

    It's built at the top with Dim strWhere As String, then it's used multiple times below, so I placed it between my Dim's and the first If statement... I got the same result as without it there.

  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,521
    Try between these two lines:

    strWhere = Left$(strWhere, lngLen)
    Me.Filter = strWhere

    That would qualify for "after it's built but before it's used"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    thanks.

    Same result, though... still get the error. Is that suppose to bring up another window? Where does that print the debug?

    Edit: Sorry, didn't have the intermediate window up... Here's what comes up there: Punch and

  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,521
    Does that help you find the problem? That is obviously an invalid string, which would look like:

    FieldName = Value AND AnotherFieldName = SomeOtherValue
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    No, that really doesn't help. I did discover that this "Punch and" was referring to the selection I made, which was "Punch and Die." The lngLen at the bottom was chopping the rest off...

    Also, is looking at this, how is it going to filter the form? It doesn't know which field to filter by. Ie. [Category] is what this needs to apply to. All of my others specify which field, but this one does not. Also, it needs to get combined with all the other selections that could possibly be made. I'm assuming that's what the AND at the end of all the other lines is for.

    I wend ahead and attached the working database. Maybe this will help.This one does not yet have the Type, Category or Subcategory combo boxes converted to List Boxes yet. The Category one is the one I'd like to focus on now. I can adapt that to the other two. The form in question is the Search form.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by scottay View Post
    Also, is looking at this, how is it going to filter the form? It doesn't know which field to filter by. Ie. [Category] is what this needs to apply to. All of my others specify which field, but this one does not.
    That's what I've been trying to tell you. Try this (replacing what you had for category):

    Code:
      Set ctl = Me.txtFilterCategory
      If ctl.ItemsSelected.Count > 0 Then
        strWhere = strWhere & "Category IN("
        For Each varItem In ctl.ItemsSelected
          strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
        Next varItem
        strWhere = Left(strWhere, Len(strWhere) - 1) 'get rid of trailing comma
        strWhere = strWhere & ") AND "
      End If
      '    If Not IsNull(Me.txtFilterCategory) Then
      '        'strWhere = strWhere & "([Category] Like ""*" & Me.txtFilterCategory & "*"") AND "
      '    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    Sorry for the delayed reply. Quittin time at work

    I pasted your code, but unfortunately still get the 2448. Looking at the immediate window, I get this: Category IN('Leads','MLC') AND. The pieces in the single quotes represent the choices I selected.

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You must have changed something else, ie took out the part at the bottom that removed the last 5 characters. Leave everything as it was, and just replace the previous code for the category combo (I have it commented out above) with what I posted. I forgot you will also need this at the beginning:

    Dim ctl As Control
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    scottay is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    14
    Thanks for your reply. I had a feeling something else got tweaked, so I decided to go back to my fresh (working) database and apply the change there.

    Success!

    Thank you so much for your help!

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

Similar Threads

  1. 2 multi select list boxes on one search form
    By woodey2002 in forum Forms
    Replies: 2
    Last Post: 11-05-2010, 12:44 PM
  2. List Boxes.
    By Willtc in forum Programming
    Replies: 2
    Last Post: 02-12-2010, 04:12 AM
  3. Replies: 1
    Last Post: 07-30-2009, 12:54 PM
  4. List box to populate other list boxes
    By Nathan in forum Forms
    Replies: 0
    Last Post: 03-03-2009, 07:22 PM
  5. Replies: 1
    Last Post: 02-25-2009, 07:29 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