MS Access Form Filter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JpjVB
    New Member
    • Aug 2010
    • 4

    MS Access Form Filter

    Hi,

    I've developed a form filter that produces a number of hours budgeted by employee/location/practice group/experience level and date available using some Allen Browne code. I have two command buttons, cmdFilter and cmdFilterAvail. cmdFilter filters the form based on the criteria selected via my list and text box filters. One of the fields in the form, text51 has a control source:=IIf([clientname]="Unassigned Time",[BudgetedHours],Null)

    On click, cmdFilter filters my results but also shows the null values present in the data set where clientname <> "Unassigned Time". I'd like cmdFilterAvail to be able to keep the filters applied, but then narrow my data set to NON-NULL text51 values thereby showing only employees with available time.

    Code for frmFilterAvail is identical to frmFilter and needs to have the NON-NULL piece added and I don't know how to do it...Help would be greatly appreciated.

    Code:
    Private Sub cmdfilteravail_Click()
      'Purpose:   Build up the criteria string from the non-blank search boxes, and apply to the form's Filter.
        'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
                            we remove the trailing " AND " at the end.
        '           2. The date range works like this: _
                            Both dates      = only dates between (both inclusive. _
                            Start date only = all dates from this one onwards; _
                            End date only   = all dates up to (and including this one).
        Dim strWhere As String                  'The criteria string.
        Dim strExperienceLevel As String
        Dim strPracticeGroup As String
        Dim strLocation As String
        Dim varSelected As Variant
        Dim lngLen As Long                      'Length of the criteria string to append to.
        Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
        
        '***********************************************************************
        'Look at each search box, and build up the criteria string from the non-blank ones.
        '***********************************************************************
        'Text field example. Use quotes around the value in the string.
        If Me.txtFilterExperienceLevel.ItemsSelected.Count > 0 Then
        For Each varSelected In Me.txtFilterExperienceLevel.ItemsSelected
        strExperienceLevel = strExperienceLevel & ",""" & Me.txtFilterExperienceLevel.ItemData(varSelected) & Me.txtFilterExperienceLevel & """"
        Next varSelected
        strExperienceLevel = Mid(strExperienceLevel, 2)  'REMOVES LEADING COMMA
        strWhere = strWhere & "([ExperienceLevel] IN (" & strExperienceLevel & ")) AND "
        End If
         
        If Not IsNull(Me.txtFilterExperienceLevel) Then
            strWhere = strWhere & "([ExperienceLevel] = """ & Me.txtFilterExperienceLevel & """) AND "
        End If
        
        If Me.txtFilterPracticeGroup.ItemsSelected.Count > 0 Then
        For Each varSelected In Me.txtFilterPracticeGroup.ItemsSelected
        strPracticeGroup = strPracticeGroup & ",""" & Me.txtFilterPracticeGroup.ItemData(varSelected) & Me.txtFilterPracticeGroup & """"
        Next varSelected
        strPracticeGroup = Mid(strPracticeGroup, 2)  'Removes leading comma
        strWhere = strWhere & "([PracticeGroup] IN (" & strPracticeGroup & ")) AND "
        End If
        
        If Not IsNull(Me.txtFilterPracticeGroup) Then
            strWhere = strWhere & "([PracticeGroup] = """ & Me.txtFilterPracticeGroup & """) AND "
        End If
        
        If Me.txtfilterlocation.ItemsSelected.Count > 0 Then
        For Each varSelected In Me.txtfilterlocation.ItemsSelected
        strLocation = strLocation & ",""" & Me.txtfilterlocation.ItemData(varSelected) & Me.txtfilterlocation & """"
        Next varSelected
        strLocation = Mid(strLocation, 2)  'Removes leading comma
        strWhere = strWhere & "([Location] IN (" & strLocation & ")) AND "
        End If
        
        If Not IsNull(Me.txtfilterlocation) Then
            strWhere = strWhere & "([Location] = """ & Me.txtfilterlocation & """) AND "
        End If
        
        'Another text field example. Use Like to find anywhere in the field.
        If Not IsNull(Me.txtFilterMainName) Then
            strWhere = strWhere & "([EmployeeName] Like ""*" & Me.txtFilterMainName & "*"") AND "
        End If
        
        'Date field example. Use the format string to add the # delimiters and get the right international format.
        If Not IsNull(Me.txtStartDate) Then
            strWhere = strWhere & "([BudgetDate] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
        End If
        
        'Another date field example. Use "less than the next day" since this field has times as well as dates.
        If Not IsNull(Me.txtEndDate) Then   'Less than the next day.
            strWhere = strWhere & "([BudgetDate] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
        End If
        
        '***********************************************************************
        'Chop off the trailing " AND ", and use the string as the form's Filter.
        '***********************************************************************
        'See if the string has more than 5 characters (a trailing " 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
    Last edited by Stewart Ross; Sep 8 '10, 08:19 PM. Reason: Please use the [Code][/Code] tags to delimit your code
Working...