|
Filter with BuildCriteria Function Posted: 14 Dec 2009 11:16 AM PST Any method that helps to find data quickly on a Form is always welcome by Users. There are several options available when you are in Form View Mode. When you Right-click on a Field a Shortcut Menu will popup showing four data filter options as shown on the sample image given below. ![]() The third option Filter For can accept a Criteria Expression like >10200 AND <=10300 or similar to filter a Range of Values from the selected field. If you would like to see more options then point on Filter option in Record Menu. There you can find two new Options: Filter by Form and Advance Filter/Sort. Filter by Form allows you to set up Criteria in more than one field to select records based on Form fields. The Advance Filter/Sort will open up Filter Design (Query Design) Window with the Source Table/Query of the Form with whatever criteria you have entered into the Filter for or Filter by Form Options earlier, if any. You can further modify the filter conditions and Sorting Order and select Apply Filter from the Records Menu to view the result. But, if you would like to build a Custom Filter Option for the User; based on a particular Field on the Form then you can use the built-in Function BuildCriteria() and write a VBA Sub-Routine around this Function to filter the data. Run the Sub-Routine on a Button Click Event Procedure and let the User input the Filter Criteria in various ways as he likes. The Usage of the Function is very simple. Let us try few examples of the Function directly in the Debug Window (Immediate Window) to understand how it works.
? BuildCriteria("OrderID",dblong,"10200") Result: OrderID=10200 The BuildCriteria() Function needs three Function Parameters. OrderID is the Data Field Name, dbLong indicates that OrderID is a Numeric Field with values of Long Integer Type and the last Value 10200 is OrderID Criteria value for selecting records. The OrderID Field Name will be inserted in appropriate locations in the Criteria expression by the BuildCriteria() Function. The last parameter value we can use in several ways depending on how we want the result. Let us see few more examples before we implement this method on a Form. Type the following expressions in the Debug Window to see how it works: ? BuildCriteria("OrderID",dblong, ">=10200 AND <10300") Result: OrderID>=10200 And OrderID<10300 ? BuildCriteria("OrderID",dblong,">=10200 AND <10300 OR >=10400 AND <10500") Result: OrderID>=10200 And OrderID<10300 Or OrderID>=10400 And OrderID<10500 Try changing the data type to dbText, for example: ? BuildCriteria("OrderID",dbText,"10200") Result: OrderID="10200" ? BbuildCriteria("OrderDate",dbDate,">10/15/2009 and <=10/31/2009") Result: OrderDate>#10/15/2009# And OrderDate<=#10/31/2009# After getting the result text from the BuildCriteria() Function all we have to do is to insert it into the Filter Property of the Form and turn ON the Filter action. Let us design a simple Form to Run our example straight away.
If you don't like to use InputBox() to prompt for Criteria Values then you may create a Text Box on the Form where Users can enter the Criteria expression before hitting on the Command Button. The limitation of the BuildCriteria() Function is that it can accept only one field as first parameter. But, there is a way to use more than one field for the Filter condition on the Form. Ask the User to enter conditions for two different fields separately and run the BuildCriteria() Function also separately to obtain the results. Join both results with AND/OR Logical operators to filter the data. The following example Code uses OrderID and ShipName field values to Filter data on the Orders Form.
The first two InputBox() Functions collects the Filter Criteria for OrderID and ShipName Field values separately. Next steps Validates the User responses and Builds the criteria strings in txtOrderFilter and txtShipNameFilter Variables. If both Variables have filter conditions in them then the User response is collected to check whether he/she needs the result set that meets both conditions (AND) or result set that meets any one of the conditions or both (OR). The Filter Strings are joined accordingly to obtain the intended result. It is not necessary that the User should always provide both set of Criteria Values (for Orderld and ShipName) all the time. They can use only one Field for entering Criteria and ignore the other. Office Assistant and MsgBox Menus-3 Office Assistant and MsgBox Menus-2 Office Assistant and MsgBox Menus Color and Picture in MsgBox Microsoft Excel Power in Access |
You are subscribed to email updates from LEARN MS-ACCESS TIPS AND TRICKS To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.