| How to create an Auto Number For Each Row of Your Query. | |
| You may need to produce a report or query which has each row of output numbered to enhance report readability or to print only every X row of output.
To create a query which has an auto number assigned field for each row of output
you use a sub-query in the auto number field. A sub-query is a query which is imbedded within your target query or report record source. They can be used to limited data in a where condition, or act as a field's data source. To create an auto number field, we use a sub-query as a field to count the number of records which occurred in underlying table or query before the current row is reached. Critical to creating the auto number sub-query is that the table or query you are using as a source must contain a unique index and this field must be included in your auto number query. As an example in an order's report, usually the order number would be a unique field used in the query or recordset. Example: To list all orders in the table, "tblOrders" and auto number the output rows, you would enter the following ina field of the orders query: RowNum: (Select Count (*) FROM [tblOrders] as Temp WHERE [Temp].[OrdNum] < [tblOrders].[OrdNum])+1If you need to constrain your query's output (as is usually the case,) to a specific set of records, you must add the same constraints to your auto number sub-query, so that temp recordset generated by the sub-query has the same result set as your main query. As an example, if you wanted the output to only list orders between a specific "StartDate" and "EndDate" you would also add the same parameters to the subquery previously shown, so that your subquery in the field would now read: RowNum: (Select Count (*) FROM [tblOrders] as Temp WHERE ((Temp.[OrdDate] BETWEEN [Startdate] AND [EndDate] ) AND ([Temp].[OrdNum] < [tblOrders].[OrdNum])))+1 |
|
| How to create a running sum query | |
| The running sum query is a simple variant on an auto number query
where the query or recordset includes a sub-query as a field to calculate the running sum. As with an auto number
query, to create a running sum requires that your query have at least one field which has
a unique value for each row.
The running sum simply uses the sub-query to sum all the values in rows where the unique field value is are less than or equal to the value of the current field. Here's an example: RunningSum: (Select Sum (OrderTotal) FROM [Orders] as Temp WHERE [Temp].[OrderID] <= [Orders].[OrderID])
|
|
| Multi-User Query and Record Set Issues. | |
When moving your database application from a development or single user environment to a multi-user environment, there are a number of issues to consider to enhance the speed of operation, data integrity and reduce record locking conflicts. Here's some general tips:
|
|
| Enhancing the Speed of Large Bulk Record Update Actions | |
| In a client server environment, when updating a large number of records
(say 2,000 or more), under transaction management in your application, you can speed up your bulk update, (and eliminate the possibility of
"out of memory" errors) by breaking down the update into smaller updates of a portion of the records.
This is because under transaction management, all the data is maintained in memory until it is committed to disk by a "CommitTrans" command. If the record set is huge, this will require significant amounts of memory, and likely heavy use of your workstation's swap file for virtual memory memory allocation. By breaking down the update into smaller transactions you can maintain the whole process in memory which enhances the speed of the update. |
|
| How to open a parameterized stored procedure as a Record Set using ADO | |
| If you haven't worked with ADO or SQL server much in the Web and ASP world, you may find the Access help file is short of examples to get you acquainted with some basic operations of working with ADO record sets if you are
using the MS Data Engine (MSDE or SQL Server 7, 2000).
One of these basic operations is opening a parameterized record set in code. To open a parameterized record set, you execute the stored procedure while assigning it to an ADO record set object. Here's an example of opening the "Sales By Year " stored procedure of the Northwind.adp sample db, via VBA code. Note that the code below is optimized for ADO 2.1, which is the least common denominator for ADO versions and the default for Access 2000 (with later versions you can use the Parameters.refresh method rather than having to specifically name and address each parameter). Dim cnn As Connection
Dim rstCom As ADODB.Command
Dim rstSales As ADODB.Recordset
Dim objPrm1 As ADODB.Parameter
Dim objPrm2 As ADODB.Parameter
Set cnn = CurrentProject.Connection
cnn.CursorLocation = adUseClient
Set rstSales = New ADODB.Recordset
Set rstCom = New ADODB.Command
rstCom.ActiveConnection = cnn
rstCom.CommandText = """Sales by Year"""
rstCom.CommandType = adCmdStoredProc
'Create parameters for your command object that
'match the stored procedure parameters.
Set objPrm1 = rstCom.CreateParameter("@beginning_date", _
adDBDate, adParamInput)
rstCom.Parameters.Append objPrm1
objPrm1.Value = "1/1/97"
Set objPrm2 = rstCom.CreateParameter("@ending_date", _
adDBDate, adParamInput)
rstCom.Parameters.Append objPrm2
objPrm2.Value = "12/31/97"
Set rstSales = rstCom.Execute
'Now you can work with the Sales By Year
'here using the rstSales object
rstSales.Close
Set rstCom = Nothing
cnn.Close
|
|
| Calculating Statistical Percentiles in a Query (or Report) | |
| Access does not contain many
common statistical functions for use in queries or reports. One of the
common statistical measures used on data sets is to calculate the median and
percentiles. There are two ways to arrive at these numbers; Option
1 is to calculate the value using standardized statistical table
values which assume a normal distribution, Option 2 is to
calculate the value using the observed data points in a specific data set.
1) You can calculate a percentile using a standardized
statistical Z-table value using Access' built Avg and StDev functions
to calculate the mean and the standard deviation. The percentile formula is:
75th percentile = .674 |
|