472,111 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

MS Access Form Filter

4
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdfilteravail_Click()
  2.   'Purpose:   Build up the criteria string from the non-blank search boxes, and apply to the form's Filter.
  3.     'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
  4.                         we remove the trailing " AND " at the end.
  5.     '           2. The date range works like this: _
  6.                         Both dates      = only dates between (both inclusive. _
  7.                         Start date only = all dates from this one onwards; _
  8.                         End date only   = all dates up to (and including this one).
  9.     Dim strWhere As String                  'The criteria string.
  10.     Dim strExperienceLevel As String
  11.     Dim strPracticeGroup As String
  12.     Dim strLocation As String
  13.     Dim varSelected As Variant
  14.     Dim lngLen As Long                      'Length of the criteria string to append to.
  15.     Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
  16.  
  17.     '***********************************************************************
  18.     'Look at each search box, and build up the criteria string from the non-blank ones.
  19.     '***********************************************************************
  20.     'Text field example. Use quotes around the value in the string.
  21.     If Me.txtFilterExperienceLevel.ItemsSelected.Count > 0 Then
  22.     For Each varSelected In Me.txtFilterExperienceLevel.ItemsSelected
  23.     strExperienceLevel = strExperienceLevel & ",""" & Me.txtFilterExperienceLevel.ItemData(varSelected) & Me.txtFilterExperienceLevel & """"
  24.     Next varSelected
  25.     strExperienceLevel = Mid(strExperienceLevel, 2)  'REMOVES LEADING COMMA
  26.     strWhere = strWhere & "([ExperienceLevel] IN (" & strExperienceLevel & ")) AND "
  27.     End If
  28.  
  29.     If Not IsNull(Me.txtFilterExperienceLevel) Then
  30.         strWhere = strWhere & "([ExperienceLevel] = """ & Me.txtFilterExperienceLevel & """) AND "
  31.     End If
  32.  
  33.     If Me.txtFilterPracticeGroup.ItemsSelected.Count > 0 Then
  34.     For Each varSelected In Me.txtFilterPracticeGroup.ItemsSelected
  35.     strPracticeGroup = strPracticeGroup & ",""" & Me.txtFilterPracticeGroup.ItemData(varSelected) & Me.txtFilterPracticeGroup & """"
  36.     Next varSelected
  37.     strPracticeGroup = Mid(strPracticeGroup, 2)  'Removes leading comma
  38.     strWhere = strWhere & "([PracticeGroup] IN (" & strPracticeGroup & ")) AND "
  39.     End If
  40.  
  41.     If Not IsNull(Me.txtFilterPracticeGroup) Then
  42.         strWhere = strWhere & "([PracticeGroup] = """ & Me.txtFilterPracticeGroup & """) AND "
  43.     End If
  44.  
  45.     If Me.txtfilterlocation.ItemsSelected.Count > 0 Then
  46.     For Each varSelected In Me.txtfilterlocation.ItemsSelected
  47.     strLocation = strLocation & ",""" & Me.txtfilterlocation.ItemData(varSelected) & Me.txtfilterlocation & """"
  48.     Next varSelected
  49.     strLocation = Mid(strLocation, 2)  'Removes leading comma
  50.     strWhere = strWhere & "([Location] IN (" & strLocation & ")) AND "
  51.     End If
  52.  
  53.     If Not IsNull(Me.txtfilterlocation) Then
  54.         strWhere = strWhere & "([Location] = """ & Me.txtfilterlocation & """) AND "
  55.     End If
  56.  
  57.     'Another text field example. Use Like to find anywhere in the field.
  58.     If Not IsNull(Me.txtFilterMainName) Then
  59.         strWhere = strWhere & "([EmployeeName] Like ""*" & Me.txtFilterMainName & "*"") AND "
  60.     End If
  61.  
  62.     'Date field example. Use the format string to add the # delimiters and get the right international format.
  63.     If Not IsNull(Me.txtStartDate) Then
  64.         strWhere = strWhere & "([BudgetDate] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
  65.     End If
  66.  
  67.     'Another date field example. Use "less than the next day" since this field has times as well as dates.
  68.     If Not IsNull(Me.txtEndDate) Then   'Less than the next day.
  69.         strWhere = strWhere & "([BudgetDate] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
  70.     End If
  71.  
  72.     '***********************************************************************
  73.     'Chop off the trailing " AND ", and use the string as the form's Filter.
  74.     '***********************************************************************
  75.     'See if the string has more than 5 characters (a trailing " AND ") to remove.
  76.     lngLen = Len(strWhere) - 5
  77.     If lngLen <= 0 Then     'Nah: there was nothing in the string.
  78.         MsgBox "No criteria", vbInformation, "Nothing to do."
  79.     Else                    'Yep: there is something there, so remove the " AND " at the end.
  80.         strWhere = Left$(strWhere, lngLen)
  81.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  82.         'Debug.Print strWhere
  83.  
  84.        'Finally, apply the string as the form's Filter.
  85.         Me.Filter = strWhere
  86.         Me.FilterOn = True
  87.     End If
  88. End Sub
Sep 8 '10 #1
0 1322

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Nothing | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.