Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Change Query Top Values Property with VBA

Introduction.

We have learned how to use the Top Values-Property of Queries (the 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 given there.

The Queries Top Values-Property 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 the design view and change the value every time when they want a different set of outputs.  Then what do we do to work with these values?

Sample SQL with TOP Property Setting.

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 that 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 is 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 the 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 depending on the Users' requirements, we can add or remove any of these three sets of values (DISTINCT, TOP nn & PERCENT) to control the output for Reports.

Preparing for the VBA-based Solution.

Our methodology to modify the SQL is very simple to implement. Collect the Query Property values from the User through a TextBox 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 introducing the VBA Routines may make it even longer.  We will complete this topic in the next blog post.

Earlier Post Link References:


Share:

No comments:

Post a Comment

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

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