Results 1 to 6 of 6
  1. #1
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58

    Filter records in a sub form from multiple combo boxes in a main form

    Been working on this for a while.
    Found some code that is partially working. I have 4 combo boxes in the main form that i want to use to filter records in a sub-form.
    It works for the first two combo boxes and i thinks it's because they are both text data. The other two consist of a number and the other a date. When i try to include one or both of these fields, the error i get is "Run-Time Error 3709" / "The search key is not found in any record".

    I currently have the code designating the variables (strJobNo and strTrainDate) as String types. When i make them a Byte and the other a Date type i get a data mismatch error when i try to filter.
    Appreciate any help to resolve this.

    Here is the code that i currently have written...
    ================================================== ================================================== =========================
    Private Sub cmdApplyFilter_Click()




    Dim strCourseTitle As String
    Dim strRosterNumber As String
    Dim strJobNo As String
    Dim strTrainDate As String


    If IsNull(Me.cboTraining.Value) Then
    strCourseTitle = "Like '*'"
    Else
    strCourseTitle = "='" & Me.cboTraining.Value & "'"


    End If


    If IsNull(Me.cboRosterNum.Value) Then
    strRosterNumber = "Like '*'"
    Else
    strRosterNumber = "='" & Me.cboRosterNum.Value & "'"
    End If


    If IsNull(Me.cboJobNumber.Value) Then
    strJobNo = "Like '*'"
    Else
    strJobNo = "='" & Me.cboJobNumber.Value & "'"
    End If


    If IsNull(Me.cboTrainingDate.Value) Then
    strTrainDate = "Like '*'"
    Else
    strTrainDate = "='" & Me.cboTrainingDate.Value & "'"
    End If




    Me.fsbTrainingRosters_New.Form.Filter = " [strTR_CourseTitle]" & strCourseTitle & " AND [strTR_RosterNumber]" & strRosterNumber & _
    " AND [bytJobNumber]" & strJobNo & " AND [dtmTR_TrainingDate]" & strTrainDate
    Me.fsbTrainingRosters_New.Form.FilterOn = True




    End Sub
    ================================================== ================================================== =========================

    Click image for larger version. 

Name:	Picture1.jpg 
Views:	14 
Size:	134.4 KB 
ID:	39955
    Last edited by jdashm; 10-16-2019 at 04:47 PM. Reason: Including image of form referred to

  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
    For starters, date data types need # as a delimiter instead of ', and numeric values don't need any delimiter (more accurately shouldn't have a delimiter). If you still have problems, use a variable and this to see how the finished filter is coming out:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    Been working with the delimiters and can't quite get the right syntax.
    Appreciate it if you could show me what it should look like based on the code i sent.
    Thanks in advance.

  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
    strTrainDate = "=#" & Me.cboTrainingDate.Value & "#"
    strJobNo = "=" & Me.cboJobNumber.Value
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    By the way, personally I'd build up a single string that only included the fields/values the user had entered.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    As Paul suggests, you can construct a single filter string for the items selected like the following...

    Code:
    If Not IsNull(Me.cboTraining) Then
    Code:
    strFilter = strFilter  & "strTR_CourseTitle = " & Me.cboTraining & " and "
    End If
    
    If Not IsNull(Me.cboRosterNum) Then
    strFilter = strFilter  & "strTR_RosterNumber = """ & Me.cboRosterNum & "'" And "
    End If
    
    ''''Do the rest of them in the same format
    
    If Nz(strFilter,"") <> "" then
    strFilter = Left(strFilter, Len(strFilter) -4)   'trim off the trailing "And "
    End if
    adjust delimiters as needed.

    edit:for some reason the code above is getting split into 2 code boxes?

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

Similar Threads

  1. Replies: 39
    Last Post: 07-28-2018, 12:27 PM
  2. Replies: 7
    Last Post: 07-01-2015, 10:29 AM
  3. Filter a Form with Multiple Combo Boxes
    By Njliven in forum Forms
    Replies: 6
    Last Post: 01-03-2013, 01:25 PM
  4. Replies: 3
    Last Post: 06-02-2012, 07:39 PM
  5. Filter form from multiple combo boxes
    By Bird_FAT in forum Programming
    Replies: 6
    Last Post: 05-19-2010, 09:32 AM

Tags for this Thread

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