<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener("load", function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <iframe src="http://www.blogger.com/navbar.g?targetBlogID=34083602&amp;blogName=LEARN+MS-ACCESS+TIPS+AND+TRICKS&amp;publishMode=PUBLISH_MODE_FTP&amp;navbarType=BLUE&amp;layoutType=CLASSIC&amp;searchRoot=http%3A%2F%2Fblogsearch.google.com%2F&amp;blogLocale=en_US&amp;homepageUrl=http%3A%2F%2Fwww.msaccesstips.com%2F" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" height="30px" width="100%" id="navbar-iframe" allowtransparency="true" title="Blogger Navigation and Search"></iframe> <div></div>
www.msaccesstips.com

LEARN MS-ACCESS TIPS AND TRICKS


International Response Fund

LEARN MS-ACCESS TIPS AND TRICKS

↑ Grab this Headline Animator

Your Ad Here
Saturday, April 18, 2009

Filter by Character and Sort

Search, Find, Filter and Sort operations are necessary to organize data into manageable form so that finding required information becomes easy. We have seen some of these actions through the following earlier Posts Titled:


Find or Filter Data on Form
Filtering Data for different Users
Animating Label on Search Success



Now, we will try a different and easy method (for the User) for filtering data by keying in one or more characters into a Text Box and obtain instant result, rather than Typing Search Text on Controls and Clicking on different Buttons or Tool Bars.


When the first character is entered into a Text Box (say the letter F) all Records with names starting with that letter will be filtered instantly. If the filtered list is large then the next character that matches can be entered to further narrow down the list and this method can be repeated.


If Backspace key is pressed to delete the last character or more characters entered in the Text Box; the filtered list will expand progressively and reverse this process. Since, this action is instantaneous the User can get the result the moment she touches a key.


If we need values from one of these filtered records transferred to some other open form then it can be done by writing a routine on the Double-Click Action at Form level as well. Double-Clicking on the left border of a particular record (Record Selector) can trigger this action and can transfer required values into a different open Form. We will see sample Code for this action at the end of this Article.


A sample image of a Form is given below.



Filter by Character and Sort example image


The only limitation is that we have created this method for use on a Tabular type Form so that several records can be viewed at one time. The earlier methods (see the references given above) were created for using on Forms with single record view.


Normally, the search or filter operation of this type will be concentrated on a single field, like the Employee Code, Company Code or Company Name; that has more relevance while looking for information.


But, here we will try to go one step further by providing a list of Field Names of the Source Object (Table or Query) attached to the Form (in a Combo Box), so that the User can select the Field that she likes to filter the data on. This will provide more choices and flexibility.


To try this example we need the Customers Table from the Northwind.mdb sample database. If you don't know the location of this file then you can find this in last week's Post, the third item out of the three links given above.


  1. Import the Customers Table.

  2. Design a Tabular Form as shown in the Image given above. I have selected only the first three fields from the Table for this example. If you would like to use all the Fields in the Table you may do so.

  3. Expand the Form Footer Section, if it is not visible (View - ->Form Header/Footer).

  4. If the Tool Box is not visible display it by selecting Toolbox from View Menu.

  5. De-select the Control Wizards Button (the top right one); if it is already in selected state on the ToolBox, so that the Wizard will not start when we select the Combo Box Tool.

  6. Select the Combo Box Tool from the Toolbox and draw a Combo Box on the Footer Section of the Form as shown in the design above.

  7. Change the Caption value of the Child Label of the Combo Box to Filter/Sort Field: .

  8. Click on the Combo Box to select it and display its Property Sheet (View- ->Properties).

  9. Change the following Property Values as given below:


    • Name = cboFields

    • Row Source Type = Field List

    • Row Source = Customers

    • Column Width = 1"


  10. Create a Text Box below the Combo Box.

  11. Change the Caption Value of the Child Label to FilterText: .

  12. Change the Name Property Value of the Text Box to FilterText.

  13. Create a Command Button to the right of the Text Box and change the following Property Values as shown:


    • Name = cmdClose

    • Caption = Close


  14. Display the VBA Code Module of the Form (View - ->Code or Alt+F11).

  15. Copy and Paste the following Code into the Module and Save the Form with the name Customers or any other name you prefer.




  16. Dim x, rst As Recordset

    Private Sub cmdClose_Click()
    DoCmd.Close
    End Sub

    Private Sub FilterText_KeyUp(KeyCode As Integer, Shift As Integer)
    Dim i As Integer, tmp

    On Error GoTo FilterText_KeyUp_Err
    i = KeyCode

    Select Case i
    Case 8 'backspace key
    Me.Refresh
    If Len(x) = 1 Or Len(x) = 0 Then
    x = ""
    Else
    x = Left(x, Len(x) - 1)
    End If
    GoSub setfilter
    Case 37, 39 'left and right arrow keys
    SendKeys "{END}" 'ignore action
    Case 32, 48 To 57, 65 To 90, 97 To 122 'space, 0 to 9, A to Z, a to z keys
    x = x & Chr$(i)
    Me![FilterText] = x
    GoSub setfilter
    End Select

    FilterText_KeyUp_Exit:
    Exit Sub

    setfilter:
    Me.Refresh
    tmp = Nz(Me!cboFields, "")
    If Len(Nz(x, "")) = 0 Then
    Me.FilterOn = False
    Else
    Me.Filter = Me![cboFields] & " like '" & x & "*'"
    Me.FilterOn = True
    End If
    Me.OrderBy = Me.RecordSource & ". " & Me!cboFields
    Me.OrderByOn = True
    Me![cboFields] = tmp
    Me.FilterText.SetFocus
    SendKeys "{END}"
    Return

    FilterText_KeyUp_Err:
    MsgBox Err.Description, , "FilterText_KeyUp()"
    Resume FilterText_KeyUp_Exit

    End Sub

    Private Sub Form_Close()
    Application.SetOption "Behavior Entering Field", 0
    Me.FilterOn = False
    Me.OrderByOn = False
    End Sub

    Private Sub Form_Load()
    Application.SetOption "Behavior Entering Field", 2
    Set rst = Me.RecordsetClone
    Me!cboFields = rst.Fields(0).Name
    rst.Close
    End Sub

  17. Open the Form in normal View.

  18. The CustomerID Field will appear as default value in the Combo Box control.

  19. Click on the Text Box below the Combo Box to set the focus on it.

  20. Type the Character F and you will see that all the records with CustomerID values starting with the letter F is filtered.


  21. If you look at the CustomerID Field values of the filtered records you may find that the second character of the CustomerIDs are different and three items have the letter R in the second character position. Besides that the CustomerIDs are correctly sorted in Alphabetical Order.


  22. Type the Character R next to the character F in the FilterText control and the list narrows with starting CustomerID characters FR. You can further filter and narrow down the list progressively this way, if needed.

  23. Press Backspace Key to delete the character R and to leave F character alone in the Text Box. The list will expand and all items starting with the letter F are back in the List.

  24. Press Backspace Key again to delete the character F from the Text Box. This time the Filter action is removed and all the records are back in the Form.



NB: The Filter Criteria Text Values are limited to the Characters 0 to 9 and A to Z only.


If you want to try the Filter action on one of the other two fields you may select that Field's name from the Combo Box above before trying the filter action explained from Step-19 onwards.


Since the CustomerIDs are created based on the CompanyName Field; trying on both those fields will give almost same result.


You may try it on the third Field ContactName for different results.


The sample code given below assumes that you have Double-Clicked on the left border of a record (Record Selector) to transfer the CustomerID code into CusID field of another open Form; Orders.



Private Sub Form_DblClick(Cancel As Integer)
Forms!Orders![CusID] = Me![CustomerID]
End Sub




StumbleUpon Toolbar



Combo Box Column Values
Drill-Down Inquiry Screen-2
Drill-Down Inquiry Screen
Command Button Animation-2
Cardinal Text Format in Access

Labels:

0 Comments:

Post a Comment

Note:Comments subject to Review by Blog Author before displaying.

Links to this post:

Create a Link

<< Home


Creative Commons License
Learn MS-Access Tips and Tricks by msaccesstips.com is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.5 India License.



This Page is best viewed with 1280 x 1024 Resolution

   FEATURED LINKS
SITEMAP
Command Button Animation
3D Headings on Forms
MsgBox & Office Assistant
Reminder Ticker
MS-Access & E-Mails
Automated E-Mail Alerts
MsgBox with Options Menu
Colorful Command Buttons
Configure Lotus Notes
Alerts through Network
Running this site has become a costly affair as the revenue from Ads is not sufficient to support it. If you find these pages informative & useful and would like to extend a helping hand, then please do it here.





Link Back to us with this Button

Learn MS-Access

Copy and Paste this HTML Code in your Webpage


Add to Technorati Favorites

Programming Blogs - Blog Catalog Blog Directory
Powered by FeedBurner
Add to Google

Software
Computers blogs
TopOfBlogs




AddMe - Search Engine Optimization Submit Your Site Free!
Go BlogZ Ave Blogs
eBlogzilla Changing LINKS
LS Blogs Blogarama
blog search directory BlogUniverse
Find Blogs in Directory RSS Directory
blogskinny.com ShowcaseBlogs.com
Amfibi

Search Engine Optimization and SEO Tools
Dmegs Web Directory Takeaway for Sale Businesses For Sale
Free Submission Directory Free site submission

Free Listing
 





Free Page Rank Checker

AddThis Social Bookmark Button

Enter your email address:

Delivered by FeedBurner



Top Blogs


Microsoft Access is the Jewell among MS-Office suite of Applications. Its Security features are excellent and works fine in Network environment. MS-Access can link/upload data from any Data Source. Applications that you design should be user-friendly and visually pleasing too. Here I would like to share my experience in Microsoft Access Programming with you and I am sure that you will find them interesting too.

My Photo
Name: Ramachandran Pillai
Location: Cochin, India

I am not an Access Guru and not through MS-Access yet. More to learn and I don’t think that aspect has any end because others have their own style of using this tool. We can learn lot more tricks, other than what we already know, from others too. My programming skills in COBOL, BASIC, Turbo-C, dBase, FoxPro, Visual Basic & Basic HTML attained through self-learning. I wrote my first COBOL Program in 1975 for ICL1901, 3rd Generation Main Frame Computer. Worked as a Computer Operator (NCR VRX8555 Mainframe Machine upto 1990) with M/s. Y.B.A. Kanoo, Saudi Arabia. Started using MS-Access Ver.2 in 1996, when dBase III+ and Foxbase (later version Foxpro) were my favorite DBMS. During Last 13 Year period I have developed more than 45 In-House Applications (medium & small) under MS-Access for our Organization, a leading Automotive Company in Oman. All the Applications are fully Secured and runs under Windows Network. It is my pleasure to share my experience with others. Anything interesting that you would like to share with me, please do. My E-mail Address: aprpillai@msaccesstips.com


If you need a Demo of any of the Topic explained here, send me an E-mail to: aprpillai@msaccesstips.com
with the Topic Description, I shall try to send a sample database to you.


Access Tips | Email | Reports | Report Tricks | Graphs | Forms | Menus | Animation | Security | Internet | How TOs | Linking | Query | Progress Meter | Alerts | Process Tips | Access Functions |




Site Designed by:www.msaccesstips.com