{"id":963,"date":"2014-07-05T15:45:34","date_gmt":"2014-07-05T19:45:34","guid":{"rendered":"http:\/\/regina-whipp.com\/blog\/?p=963"},"modified":"2020-04-18T17:13:25","modified_gmt":"2020-04-18T21:13:25","slug":"filterforms","status":"publish","type":"post","link":"https:\/\/regina-whipp.com\/blog\/?p=963","title":{"rendered":"Filtering Forms&#8230;"},"content":{"rendered":"<p>Here are three ways to filter records on a Single Form or a Continuous Form. (In these examples no Subforms were used.)<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 1<br \/>\n<\/strong><\/span>To toggle back and forth between (Yes\/No Data Type) All and Completed (Tasks, Projects, Orders, etc.)&#8230;<\/p>\n<ul>\n<li>Step 1<\/li>\n<\/ul>\n<p>In the <strong>Header<\/strong> of you <strong>Form<\/strong> place an UNBOUND Check Box and name it <strong>chkMyFilter<\/strong><\/p>\n<ul>\n<li>Step 2<\/li>\n<\/ul>\n<p>In the After_Update event of the UNBOUND Check Box copy\/paste everything between the Private Sub and End Sub lines below. After you have copied it change <strong>pDone<\/strong> to the name of your Yes\/No field in your Table, not the name of the field Control name on the Form.<\/p>\n<p>Private Sub chkMyFilter_AfterUpdate()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n\u00a0\u00a0\u00a0 If Me.chkMyFilter = True Then\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.Filter = &quot;pDone = &quot; &amp; vbTrue\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.FilterOn = True\r\n\u00a0\u00a0 Else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Me.Filter = &quot;&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.FilterOn = False\r\n\u00a0\u00a0 End If\r\n<\/pre>\n<p>End Sub<\/p>\n<p>Close and save (or save and then close) and try it out.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 2<br \/>\n<\/strong><\/span>In this example you can use values from a look-up table to filter your records. This allows a lot more flexibility because I can add\/edit and\/or subtract items from the list via the table. My table is&#8230;<\/p>\n<p style=\"padding-left: 30px;\">tblSources<\/p>\n<ul>\n<li>Step 1<\/li>\n<\/ul>\n<p>In the Header of you Form place an UNBOUND Combo Box and name it cbo<strong>S<\/strong>SourceID. (The double S is not a mistake, it is because in the Detail section of my Form I have a Combo Box named cboSourceID, so, I put an additional <strong>S<\/strong> to indicate this is my cbo*<strong>Search<\/strong>*SourceID used to Filter my records.<\/p>\n<ul>\n<li>Step 2<\/li>\n<\/ul>\n<p>Set the Row Source of cboSSourceID the same as the Combo Box in the Detail section of your Form. For this example I used&#8230;<\/p>\n<p style=\"padding-left: 30px;\">SELECT tblSources.sSourceID, tblSources.sSource<br \/>\nFROM tblSources<br \/>\nWHERE (((tblSources.sActive)=True))<br \/>\nORDER BY tblSources.sSortOrder;<\/p>\n<p><strong>Other settings<\/strong>:<\/p>\n<p style=\"padding-left: 30px;\">Column Count =2<br \/>\nBound Column = 1<br \/>\nColumn Widths &#8211; 0&#8243;;1&#8243;<\/p>\n<ul>\n<li>Step 3<\/li>\n<\/ul>\n<p>In the After_Update event of the UNBOUND Combo Box copy\/paste everything between the Private Sub and End Sub lines below. After you have copied it change <strong>pSourceID<\/strong> to the field name in your Table, not the name of the field control name on the Form and in your Table and change the <strong>cboSSourceID<\/strong> to the name of your UNBOUND Combo Box in the Header.<\/p>\n<p>Private Sub cboSSourceID_AfterUpdate()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n\u00a0\u00a0 Me.Filter = &quot;&#x5B;pSourceID] = &quot; &amp; Me.cboSSourceID\r\n\u00a0\u00a0 Me.FilterOn = True\r\n<\/pre>\n<p>End Sub<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Step 3a<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>To clear the Filter you can use the Double_Click of <strong>cboSSourceID<\/strong>&#8230;<\/p>\n<p>Private Sub <strong>cboSSourceID<\/strong>_DblClick(Cancel As Integer)<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n\u00a0\u00a0\u00a0Me.Filter = &quot;&quot;\r\n\u00a0\u00a0 Me.FilterOn = False\r\n\u00a0\u00a0 Me.cboSSourceID = &quot;&quot;\r\n<\/pre>\n<p>End Sub<\/p>\n<p>&#8230;or add the above code a <strong>Clear Filter<\/strong> Command Button.<\/p>\n<p>Close and save (or save and then close) and try it out.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 3<br \/>\n<\/strong><\/span>This example allows you to create a Query as much or as little criteria as you like for the filter. You can use one Command Button to toggle back and forth between All\/Filtered, for this example I am using one Command Button using the Caption property of the Command Button to toggle back and forth.<\/p>\n<ul>\n<li>Step 1<\/li>\n<\/ul>\n<p>Create two Record Sources, one that will show all the Records, such as the Table and another that shows just the filtered Records such as a Query, i.e.:<\/p>\n<p style=\"padding-left: 30px;\">tblPending for all the Records<br \/>\nqryPending for the filtered Records.<\/p>\n<p>qryPending<br \/>\nSELECT tblPending.pPendingID, tblPending.pActionID, tblPending.pQuantity, tblPending. pReceiveDate, etc&#8230;<br \/>\nFROM tblPending<br \/>\nWHERE (((tblPending.pActionID) Is Null) AND ((tblPending.pReceiveDate) Is Null));<\/p>\n<ul>\n<li>Step 2<\/li>\n<\/ul>\n<p>Set the Record Source of your Form using the *filtered* query. In this example that would be qryPending. (If you want to open to showing ALL records you will need to reverse the code!)<\/p>\n<ul>\n<li>Step 3<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-966 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/07\/ShowAll.png\" alt=\"ShowAll\" width=\"93\" height=\"21\" \/><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-965 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/07\/Filtered.png\" alt=\"Filtered\" width=\"93\" height=\"21\" \/><\/p>\n<p>Place a Command Button in your Forms Header and name it <strong>cmdShowAll<\/strong>. If the Wizard pops up, just press Cancel, we are going to put our own code there so we won\u2019t be using any of the Wizards code. Change the Caption property to say \u201c<strong>SHOW ALL<\/strong>\u201d. For my Command button I used these settings&#8230;<\/p>\n<p style=\"padding-left: 30px;\">Font Name= Tahoma<br \/>\nFont Size = 7<br \/>\nFont Weight = Bold<\/p>\n<p>Side note:\u00a0 The buttons will change Caption depending on which Record Source is showing, which is why you only need to add one Command Button.\u00a0 Make sure you follow Step 4 and Step 5 for the Caption to toggle properly.<\/p>\n<ul>\n<li>Step 4<\/li>\n<\/ul>\n<p>At the top of your Forms Module under&#8230;<\/p>\n<p style=\"padding-left: 30px;\">Option Compare Database<br \/>\nOption Explicit<\/p>\n<p>Side note: If those two lines are not at the top of your Module put them there, particularly, Option Explicit which will *stop* when it finds an undeclared variable(s).<\/p>\n<p>Mine says SHOW ALL or COMPLETED ONLY but yours can say whatever you want. Just remember to make sure the Command Button is wide enough to show the longest Caption.<\/p>\n<p>&#8230;place these two lines<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nConst ShowAll = &quot;SHOW ALL&quot;\r\nConst ShowFiltered = &quot;COMPLETED ONLY&quot;\r\n<\/pre>\n<ul>\n<li>Step 5<\/li>\n<\/ul>\n<p>In the On_Click event of the Command Button copy\/paste everything between the Private Sub and End Sub lines below.<\/p>\n<p>Private Sub cmdShowAll_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nOn Error Resume Next  'Lazy mans error code\r\n\r\n\u00a0\u00a0 Select Case Me.cmdShowAll.Caption\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Case ShowFiltered\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.RecordSource = &quot;qryPending&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.cmdShowAll.Caption = ShowAll\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Case ShowAll\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.RecordSource = &quot;tblPending&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Me.cmdShowAll.Caption = ShowFiltered\r\n\u00a0\u00a0 End Select\r\n<\/pre>\n<p>End Sub<\/p>\n<p>Close and save (or save and then close) and try it out.<\/p>\n<p><span style=\"color: #ff6600;\"><strong>NOTE<\/strong><\/span>: When filtering a Subform use&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nOn Error Resume Next  'Lazy mans error code\r\n\u00a0\u00a0    Me.NameOfSubform.Form.Filter =&quot;&#x5B;FieldBeingSearchedOn] = '&quot; &amp; Me!txtYourSearchTextBox &amp; &quot;'&quot;\r\n      Me.NameOfSubform.Form.FilterOn = True\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Here are more filtering examples&#8230;<\/p>\n<p><a href=\"http:\/\/allenbrowne.com\/ser-62.html\" target=\"_blank\" rel=\"noopener noreferrer\">Allen Browne\u2019s Search Form<\/a><br \/>\n<a href=\"http:\/\/allenbrowne.com\/ser-28.html\" target=\"_blank\" rel=\"noopener noreferrer\">Allen Browne\u2019s Filter a Form on a Field in a Subform<\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_963\" class=\"pvc_stats all  \" data-element-id=\"963\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/regina-whipp.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here are three ways to filter records on a Single Form or a Continuous Form. (In these examples no Subforms were used.)<\/p>\n<p>Example 1 To toggle back and forth between (Yes\/No Data Type) All and Completed (Tasks, Projects, Orders, etc.)&#8230;<\/p>\n<p> Step 1 <\/p>\n<p>In the Header of you Form place an UNBOUND Check Box and name [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_963\" class=\"pvc_stats all  \" data-element-id=\"963\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/regina-whipp.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,63],"tags":[64,18,54,31],"class_list":["post-963","post","type-post","status-publish","format-standard","hentry","category-access-tips","category-database-design","tag-access-tips","tag-database-design","tag-filter","tag-forms","odd"],"a3_pvc":{"activated":true,"total_views":15004,"today_views":0},"_links":{"self":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=963"}],"version-history":[{"count":7,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/963\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}