Returns a string representing the calculated range that contains a number.
Public Function Partition( _ ByVal Number As Long, _ ByVal Start As Long, _ ByVal Stop As Long, _ ByVal Interval As Long _ ) As String
Exception type | Error number | Condition |
---|---|---|
5 | Start < 0, Stop <= Start, or Interval < 1. |
The Partition function calculates a set of numeric ranges, each containing the number of values specified by Interval. The first range begins at Start, and the last range ends at Stop. The Partition function then identifies which range contains Number and returns a string describing that range. The range is represented in the string as "lowervalue:uppervalue", where the low end of the range (lowervalue) is separated from the high end (uppervalue) by a colon (:).
If necessary, the Partition function inserts leading spaces before lowervalue and uppervalue so that they both have the same number of characters as the string representation of the value (Stop + 1). This ensures that if you use the output of the Partition function with several values of Number, the resulting text will be handled properly during any subsequent sort operation.
The following table shows some sample strings for ranges calculated using three sets of Start, Stop, and Interval. The "First range" and "Last range" columns show the lowest and highest ranges possible given the values of Start and Stop. The "Before first range" and "After last range" columns show the strings returned for values of Number less than Start and greater than Stop, respectively.
Start | Stop | Interval | Before first range | First range | Last range | After last range |
---|---|---|---|---|---|---|
0 | 99 | 5 | " : -1" | " 0: 4" | " 95: 99" | "100: " |
20 | 199 | 10 | " : 19" | " 20: 29" | "190:199" | "200: " |
100 | 1010 | 20 | " : 99" | " 100: 119" | "1000:1010" | "1011: " |
In the preceding table, the third line shows the result when Start and Stop define a set of numbers that cannot be evenly divided by Interval. The last range ends at Stop, making it only 11 numbers long, even though Interval is 20.
If Interval is 1, the range is "Number:Number", regardless of the Start and Stop arguments. For example, if Number is 267, Stop is 1000, and Interval is 1, Partition returns " 267: 267".
Partition is useful in database queries. You can create a select query that shows how many orders occur within various value ranges, for example with invoice values from 1 to 1000, 1001 to 2000, and so on.
This example assumes you have an Orders table that contains a Freight
field. It creates a SELECT procedure that counts the number of orders for which freight cost is within each of several ranges. The Partition function first establishes these ranges, and then the SQL Count function counts the number of orders in each range. In this example, the arguments to the Partition function are Start = 0, Stop = 500, Interval = 50. The first range is therefore 0:49, and so on up to 450:499 and 500:500.
SELECT DISTINCTROWPartition(
[Freight],0, 500, 50)
As Range, Count(Orders.Freight) As Count FROM Orders GROUP BYPartition(
[Freight],0,500,50)
;