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:
- Open the Query Definition and read the existing SQL String.
- Scan the SQL string and look for the text DISTINCT, TOP nn and PERCENT. If found then remove them from the SQL String.
- Validate the input given by the User in the Textbox and checkbox and insert appropriate SQL Clauses in the SQL String.
- 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.
- GetRows() Function and Exporting Data
- Create Menus with Macros-2
- Create Menus with Macros
- Join Split Array Function-2
- Join Split Array Function