Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.


We have seen the usage of different type of complicated Queries like:

Today we will learn how to define and extract the top 100 or whatever number of records or certain percentage of the total records based on values in a particular Column.

You need to know only few rules to work with this type of Queries.

  1. You may select several Columns of data from source for output.
  2. You must sort one or more columns of data in Ascending/Descending Order and the left-most sorted column will pick the top value records.
  3. If the output values have duplicates (duplicate values in all columns in two or more records) then you can set the Unique Values Property to Yes (DISTINCT clause in the SELECT statement) to suppress duplicate records.
  4. If the Query has more than one Table/Query as source  and when duplicate records are found in the output; set the Unique Records Property value to Yes (DISTINCTROW clause in the SELECT statement) to suppress duplicate records.

Let us try an example:

  1. Open a new database or one of your existing databases.
  2. Import the Order Details and Products Tables from Northwind.mdb sample Database. The Products table is not directly used but there is a lookup reference to this table in the Order Details table for Product Name.
  3. Open a new Query in SQL View (without selecting a Table/Query from the displayed list).
  4. Copy and paste the following SQL string into the SQL editing window and save the Query with the name Order_DetailsQ.
    SELECT TOP 100 [Order Details].ProductID, [Order Details].UnitPrice
    FROM [Order Details]
    WHERE ((([Order Details].OrderID) Between 10248 And 10300))
    ORDER BY [Order Details].UnitPrice DESC;
  5. Open the Query in Design View and check the order of placement of fields and the Sort Field.
  6. Right-click on an empty area above the column grid to display the Query Shortcut Menu and select Properties… option to display the Property Sheet. Check the image below:
  7. Check the Top Values Property that set with the value 100, which dictates the Query to select 100 records with highest Unit Price values.
  8. Change the View of the Query into Datasheet View to display the output records.  See the image given below:

    The Order Details Table have several records of the same Product under different OrderIDs. We have purposely not included the OrderID field in the data column except in criteria to select the records of OrderIDs between 10248 and 102300 and to pick some duplicate records.  As you can see in the image given above; there are several duplicate records of the same product in the output.  With the duplicate records we can try out the Unique Values property settings.

  9. Change the Top Values property value to All and change the Query into Datasheet View. The output will be about 150 records for OrderIDs between 10248 and 102300.
  10. Change the Query into Design View and display its Property Sheet.
  11. Set the Top Values property with 100 and Unique Values property with Yes.
  12. Change the Query into Datasheet View and inspect the output.

    Now the duplicate records are suppressed (29 of them) and the output is now only 71 records.  The next property Unique Records can be set to Yes to get the same result when data fields are placed from two or more Tables or Queries joined together in the Query design and duplicate records are found in the output; due to one-to-many relationship.

    We have specified 100 records in the Top Values Property but the Unique Values property setting reduced the number of records into 71 after suppressing duplicates. 

  13. Change the Top Values Property setting from 100 into 25% and change the View into Datasheet View.

The percentage setting gives only one-fourth (18 records) of Total 71 records as output with the Unique Values setting to Yes or 39 records (one-fourth of about 150 records) with the Unique Values setting to No.

The Top Values Property setting can be a specific number or a percentage of Total Records.


1 comment:

  1. Highly challenging thanks, There's no doubt that your trusty audience would definitely want a lot more items like that keep up the great work.


Comments are subject to moderation before publishing.


Subscribe in a reader
Your email address:

Delivered by FeedBurner

Blog Directory Visit blogadda.com to discover Indian blogs


Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email


Forms How Tos Functions MS-Access Security Reports msaccess forms Animations msaccess animation Class Module Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Array Queries msaccess reports External Links msaccess tips Accesstips Downloads Objects Property Menus and Toolbars Controls Event MsaccessLinks Process Controls Art Work Collection Object WithEvents msaccess How Tos Combo Boxes Graph Charts List Boxes VBA msaccessQuery Command Buttons Command Button Data Dictionary Object Emails and Alerts Form Query RaiseEvent Report Calculation Custom Wizards DOS Commands Data Type ms-access functions msaccess functions msaccess graphs msaccess reporttricks Item Object Reference msaccessprocess security advanced Access Security Add Custom Functions Field Type Fields Form Instances Key Macros Menus SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Auto-Number Copy Expression Join Methods Microsoft Numbering System Records Recordset Security Split Table Time Difference Utility Workgroup Wrapper Classes database ms-access msaccess wizards reference text tutorial vba code Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands function hyperlinks iSeries Date iif msaccess msaccess alerts pdf files restore switch toolbar updating upload

Featured Post

WithEvents and All Form Control Types

Introduction So far we have worked extensively with only one type of Control: the TextBoxes on Form, enabled with Events to Raise and captur...


Blog Archive

Recent Posts