|
Filter Function Output In Listbox-2 Posted: 25 Sep 2009 05:01 AM PDT Last week we have seen the usage of Filter() Function with a simple example and I hope you understood how it works. We have assigned constant values to the Source Array elements directly to keep the VBA Code as simple as possible. We can filter data on Forms by setting Criteria on the Filter Property of Forms. Similarly, we can use conditions in Queries to filter information from Tables as well. But, the Filter() Function gives a unique way of filtering data from an Array of information loaded from Tables or Queries and create output quickly based on matching or non-matching options. Let us try out the Filter() Function in an Address Book Application to quickly find persons or places that matches with the specified search text and display them in a List Box. We will use Names and Addresses from the Employees Table of Northwind.mdb sample database for our experiment. Following is the User Interface design that we planned to create and explains how the User will interact with it to display information quickly on the Form. We will design a Form with a List Box, a Text Box Control, a Check-Box Control and a Command Button for our experiment. An image of such a Form in Design View is given below: When the Form is open in normal view the List Box and Text Box Controls will be empty. The User can enter the word ALL in the Text Box Control and click the Command Button to display the Name and Addresses of all Employees in the List Box. Or The User can enter a word or phrase, like part of a Name or Address, that can match anywhere within the Name and Address text, and click on the Command Button to filter out the matching items and to display them in the List Box. If the Matching Cases Check-Box is in selected state then the Filter action will select records that matches with the search text given in the Text Box Control otherwise it will select all records that do not match with the search text. To provide the User with the above facility we need two Subroutines on the Form's Code Module and a User Defined Function in Standard Module of the Database to use the Filter() Function. When the User opens the above Form the first Sub-Routine is run from the Form_Load() EventProcedure to read the data (First Name, Last Name & Address) from Employees Table, join all the three field values into a single row of text and load them into a Singly Dimensioned Array Variable in Memory. This data will remain in memory till the User closes the Form. The second Sub-Routine is run when the User clicks on the Command Button to extract information from the Source Array with the help of Filter() function, based on the search text entered into the Text Box Control. The Filter() Function will extract the entries that matches with the search Text in the Text Box Control, from the Source Array Variable, and save the output into the target variable xTarget. All we have to do is to take these values, format and insert as Row Source Property Value to display them in the List Box. Let us prepare for the Address Book's Quick Find Project.
NB: Ensure that the Name Property Values of the above controls are given exactly as I have mentioned above. This is important because we are referencing these names in Programs.
We cannot use the Filter() Function in the Form Module because the function name clashes with the Form Property Filter. We have inserted the Filter() Function in the Standard Module enveloped in a User Defined Function GetFiltered() with necessary Parameters so that we can call it from Form Module. The first parameter to the Function is passed By Reference so that it can use the Source Array values directly. This action will display the Name and Addresses of all Employees from the xSource() Array loaded from the Employees Table. This time you will find that only two Employee (Nancy & Laura) names and addresses are filtered and the word Ave is appearing in their Address Lines. Now, all items except the lines with the word Ave are listed in the List Box. If you go through the Programs that we have copied into the Form Module you can see that we have declared the xSource() Array Variable in the Global area of the Module so that we can use the data in both Sub-Routines in the Form Module. In the Form_Load() Event Procedure we have declared three Variables as fixed length String Type (see the declaration line given below). Dim FName As String * 12, LName As String * 12, Add As String * 20 When we read employee Name and Address into these Variables the values will be left justified inside the Variable and balance area of the declared size will be space filled to the right. This method will space out items in fixed distance from each other and properly aligned when displayed. It is important that we use a Fixed Width Font, like Courier New, for the List Box display and we set this in Step-4 above. If you click the Filter Command Button when the Text Box is empty then the program terminates, otherwise it calls the GetFiltered() Function by passing the parameter values. The output Values are returned in the xTarget Array and the next steps formats the Value List and displays them in the List Box. Hiding Report Lines Conditionally-3 Hiding Report Lines Conditionally-2 Hiding Report Lines Conditionally Network and Report Page Setup-3 Network and Report Page Setup-2 |
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.