<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
Thursday, March 13, 2008

Refresh Dependant Combo Box Contents

Creating a Combo box on a Data Entry/Editing Screen is easy. Click on the Combo box Tool Button on the Toolbox, select the required Fields from the Source Table or Query on the Wizard, select the target field on the Form and you are through. Well, may be not quite, few more changes may require on the Property Sheet of the combo box to adjust the width of individual Columns of List, Total List Width property, List Rows Property, Limit to List and On Not in List Event Procedure to prevent entering values other than not defined on the combo box.


There are instances of more than one combo box on a Form and it is likely that the contents of one combo box depends on the values of another one. In such situations limiting the contents of the second combo box with the data related to the current item selected on the first combo box is more appropriate, rather than displaying all of them always. The User can then select the item that he wants from a limited list instead of scrolling through a lengthy list to find the required one.


With few lines of Code on the On Click Event Procedure of the first combo box will do the trick and can make life easier for the user. He/She can do more work with the extra time otherwise spent on scrolling the combo box.


We need three Tables from the Northwind.mdb sample database for our test run. We are hitting this database more often to take ready-made data for our examples. If you are not sure where you can find this sample database in your PC, visit the page Saving Data on Form not in Table for its location references.


Beginners of Microsoft Access can find plenty of sample Tutorial materials in this database to keep them engaged for some time and to learn the basics of database design. When you grow out of it or you want something different and more interesting then start hitting this site for help. I will be happy if you find time for a change of scene and drops in here for clues. I have plans to keep you occupied for a long time.



  1. Import the following Tables from the above sample database into your current project:


    • Categories

    • Products

    • Order Details


  2. Click on the Order Details Table, select Form from Insert Menu and select Auto Form: Columnar from the Wizard and click OK.


  3. Save the Form with the name Order Details.


  4. Before going for changes on the Form let us have a look at the contents appearing on the Form just now created. Open the Form in normal view. There is already a Combo box on the Form for ProductID Field.


    Click on the Combo box and scroll down the list and read through some of the items appearing in the list, if you can understand them. To tell you the truth many of the names appearing in there are alien to me, especially the ones with one or more dots above the letters, besides others. But, one thing I am sure about is that they belong to different Category (we have imported the table) of items and they all got mixed up in that list.


    We will create another Combo box to organize and show them in a better way. We will now proceed with the design changes to the Form.


  5. Select Design View from View Menu, if you are still keeping the Form open in normal view. If you are not comfortable without a heading for your Form, expand the Header Section of the Form create a Label there and change the caption to Order Details and change the Font size to make the heading look like a Heading.


  6. Select all the controls and labels except the OrderID control and Label, drag them down to make room to create a combo box below the OrderID field.


  7. You can select all the controls by clicking somewhere down on an empty area on the form, hold the mouse button down and drag over all the controls to select them except the OrderID field and label. When you are sure that you have covered all the controls and all of them are selected release the mouse button. Hover the mouse pointer over the selected controls to turn the mouse pointer to the image of a hand. Now click and hold the mouse pointer and drag them down to make room for another field and label above. We are going to create another Combo box with the source data from the Categories Table.


  8. If the Toolbox is not visible select Toolbox from View Menu. Make sure the Control Wizard button (top right button) on the Toolbox is selected.


  9. Click on the Combo box button on the Toolbox and draw a Combo box above the ProductID combo box, about the same size of ProductId combo box.


  10. Select the Categories Table from the displayed list and click Next.


  11. Click the >> button to select both fields displayed from the Categories Table and click Next, and Next again.


  12. On the displayed control ensure that the option Remember the Values for later use (in our Code) is selected and click Finish.


  13. Combo Box Design

    We have not created a new field on the Order Details table to store the value from this combo box, when selected. That is not necessary here because we will take the Product Category Code inserted by the user into the Unbound Textbox (combo box) and use it to filter the contents of the Products combo box. The item selected from the Products combo box is only stored into the Order Details Table.


  14. Click on the combo box, display the Property Sheet (View - -> Properties) and change the Name property to cboCat.

  15. Change the size of the Label control to match with the size of others.

  16. Display the Form’s Code Module. (View - - > Code)

  17. Copy and paste the following Code in the VBA Module:



  18. Private Sub cboCat_Click()
    Dim xsql0 As String, xsql2 As String, xsql As String
    Dim crit As String

    xsql0 = "SELECT DISTINCTROW [ProductID], " _
    & "[ProductName] FROM Products WHERE ("

    xsql2 = " ORDER BY [ProductName];"

    crit = "[CategoryID] = " & cboCat & ") "

    xsql = xsql0 & crit & xsql2

    Me.ProductID.RowSource = xsql
    Me.ProductID.Requery

    End Sub

  19. Save and close the VBA Window (File - -> Close and Return to Microsoft Access). Save the Order Details Form with the changes.

  20. Open the Form in normal view. Click and select an item from the new Combo box, say Meat/Poultry.

  21. Now click the Products Combo box. The List of items appearing there belongs to the Meat/Poultry Category only. Experiment with other Category of items also.



How it works


Even though the Combo Box that we have created displays only one Column of Items (Description) we have selected two Fields CategoryID and CategoryName from the Categories Table through the Combo Box Wizard. If you display the property sheets of both Combo Boxes and look at the Column Widths Property you can see that the value set for the first Column width is zero in both cases, which will hide the CategoryID Code Number that we are selecting by clicking the Description. The Description is more important to the User and easy to understand which item he/she is selecting.


But, when a selection is made in the combo box, CategoryID number corresponding to that item is recorded in the Combo Box control. We are using this number to filter the items that belongs to this category from the Products Table by building a SQL string and using the CategoryID Value as criterion. This SQL string is used as Row Source for the Products Combo Box.


If you open the Products Table and view the Data, you can see that Category Description is part of this Table also. When you click on one of the Category field you can see that it is a Combo Box. Here also the same kind of Combo box is created (no filtering of data) while designing the Table Structure to display the Products Category from the Category Table.


Open the Products Table in Design View. Click on the CategoryID field and select the Lookup Tab from the Property Sheet displayed below. On the Property Sheet you can see similar settings that we found on our Combo box on the Form. The CategoryID Source Data for the Combo box on the Products table is taken from the Category Table and Column width Property is set to zero to hide the numeric code and to display the Category Name instead.


NB: Any suggestions for improvement or showing a better method to arrive at the same results are welcome.



Progress Bar on Form
Progress Meter
Keyboard Shortcuts
Find or Filter Data on Form
Who is Online

Labels:

2 Comments:

Anonymous Bill Beck said…

Congratulations!!! You have been nominated for a 2007 Best Of Blog Award!! Especially designed to bring attention to lower profile bloggers, The BoB’s as we like to call them are currently taking nominations in over 20 different categories. To find out more about how your site has a chance to become one of this year’s Best Of’s and how to nominate other bloggers, visit us at www.thebestofblogs.com. Remember voting begins April 14th so make sure you pass the word to your friends, family, and faithful followers.

Sincerely,
Bill Beck
Project Mgr.
The Best Of Blog Awards
Email:Bloggerbeck@aol.com

March 16, 2008 4:00 AM  
Blogger a.p.r. pillai said…

Thank you Mr. Bill Beck.

March 20, 2008 4:05 PM  

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