Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Change Query Top Values Property with VBA

We have learned how to use the Top Values Property of Queries (SELECT, MAKE-TABLE and APPEND Queries where this property is applicable) in an earlier Post.  Before going through this Article I suggest you take a look at the earlier one.  To do that click here.

The Top Values Property value of the Query can be set only manually during design time .  You cannot address the Top Values Property of the Query in VBA, to change its present value dynamically.  You cannot ask the User to open the Query in design view and change the value every time when they want a different set of output.  Then what do we do to work with these values?

When we change the Top Values property value manually MS-Access modifies the SQL of the Query to reflect that change.  Keeping this behavior of the Query in mind we can play some tricks with VBA to manipulate the Query's SQL to get what we want, rather than looking for the Property to set the value.  But, before doing that let us examine and find out what happens to the SQL definition of the Query, when you set Top Values and other Properties.

Here is a sample SQL of a SELECT Query with the Top Values Property set with the Value 25.

SELECT TOP 25 Orders.OrderID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.Freight
FROM Orders
ORDER BY Orders.Freight DESC;

As you can see in the above example; immediately after the SELECT clause the text TOP 25 is inserted in the SQL string indicating that the TOP Value Property of the Query is set with the value 25.  In the ORDER BY Clause you can see that the Freight Column is sorted in Descending Order.  The result set of the Query is 25 records with the highest Freight Values in the Table.

When you want 25% of the Total records as output from the Orders Table then the Top Values property value must be set as 25%, rather than 25.  The SQL text changes to SELECT TOP 25 PERCENT . . . Sample SQL is given below:

SELECT TOP 25 PERCENT Orders.OrderID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.Freight
FROM Orders
ORDER BY Orders.Freight DESC;

The next one that affects the record set is Unique Values Property (valid values are Yes or No).  When it is set; suppresses duplicate records from the output displayed in the Datasheet. But, other field values which are not placed on the Query Column from the Table are not considered while excluding duplicate records.  The DISTINCT Clause is inserted in the SQL immediately after the SELECT Clause.  The modified sample SQL is given below after the Unique Value Property set to Yes.

SELECT DISTINCT TOP 25 PERCENT Orders.OrderID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.Freight
FROM Orders
ORDER BY Orders.Freight DESC;

There is one more Query Property Unique Records that can be set (valid values are Yes or No) to suppress duplicate records from output.  When this is set with the value Yes it suppresses duplicate records, based on values in all the fields in the source Table, irrespective of their placement on the Query column.  In this case the DISTINCT clause will be modified as DISTINCTROW in the SQL string.

SELECT DISTINCTROW TOP 25 PERCENT Orders.OrderID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.Freight FROM Orders ORDER BY Orders.Freight DESC;

This property we will exclude from our VBA based solution. As you can see from the above examples that depending on the Users' requirement we can add or remove any of these three set of values (DISTINCT, TOP nn & PERCENT) to control the output for Reports.

Our methodology to modify the SQL is very simple to implement. Collect the Query Property values from the User through a Text Box and a Checkbox on a Form. Let the User click a Command Button to redefine the Query.

This will invoke the following actions to redefine the Query:

  1. Open the Query Definition and read the existing SQL String.
  2. Scan the SQL string and look for the text DISTINCT, TOP nn and PERCENT.  If found then remove them from the SQL String.
  3. Validate the input given by the User in the Textbox and checkbox and insert appropriate SQL Clauses in the SQL String.
  4. Update the modified SQL in the Query definition.

This article has become too long now.  Explaining the above four steps and introduction of the VBA Routines may make it even longer.  We will complete this topic in the next blog post.


No comments:

Post a Comment

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