语 法:
Var (expr)
VarP (expr)
expr 占位符代表一个字符串表达式(它标识了包含要计算的数值数据的字段)或表示使用该字段的数据执行计算的表达式。 expr 中的操作数可以包括表字段、常量或者函数(可以是固有函数或用户定义的函数,但不能是其他 SQL 聚合函数)的名称。
备 注:
VarP 函数计算样本总体,Var 函数计算样本总体抽样。
如果基础查询中包含了两个以下个记录,那么 Var 和 VarP 函数返回 Null 值,这表示无法计算方差。
在 查询表达式 或 SQL 语句中使用 Var 和 VarP 函数。
示 例:
以下示例使用 orders 表估算发往 United Kingdom 的订单的运费的偏差。
以下示例调用 EnumFields 过程,您可以在 Select 语句示例中找到该过程。
Sub VarX()
Dim dbs As Database, rst As Recordset
' 在你的电脑上修改这一行包含Northwind的路径
Set dbs = OpenDatabase("Northwind.mdb")
' 订单被运往英国,计算运费的方差
Set rst = dbs.OpenRecordset("Select " _
& "Var(Freight) " _
& "AS [UK Freight Variance] " _
& "FROM orders Where ShipCountry = 'UK';")
' 填充记录集
rst.MoveLast
' 调用 EnumFields 打印记录集。 传递 Recordset 对象和所需字段宽度
EnumFields rst, 20
Debug.Print
Set rst = dbs.OpenRecordset("Select " _
& "VarP(Freight) " _
& "AS [UK Freight VarianceP] " _
& "FROM orders Where ShipCountry = 'UK';")
' 填充记录集。
rst.MoveLast
EnumFields rst, 20
dbs.Close
End Sub
Sub EnumFields(rst As Recordset, intFldLen As Integer)
Dim lngRecords As Long, lngFields As Long
Dim lngRecCount As Long, lngFldCount As Long
Dim strTitle As String, strTemp As String
' 将 lngRecords 变量设置为记录集中的记录
lngRecords = rst.RecordCount
'将 lngFields 变量设置为记录集中的字段。
lngFields = rst.Fields.Count
Debug.Print "There are " & lngRecords _
& " records containing " & lngFields _
& " fields in the recordset."
Debug.Print
' 形成一个字符串来打印列标题.
strTitle = "Record "
For lngFldCount = 0 To lngFields - 1
strTitle = strTitle _
& Left(rst.Fields(lngFldCount).Name _
& Space(intFldLen), intFldLen)
Next lngFldCount
' 打印列标题.
Debug.Print strTitle
Debug.Print
rst.MoveFirst
For lngRecCount = 0 To lngRecords - 1
Debug.Print Right(Space(6) & _
Str(lngRecCount), 6) & " ";
For lngFldCount = 0 To lngFields - 1
' 检查空值.
If IsNull(rst.Fields(lngFldCount)) Then
strTemp = ""
Else
' 将 strTemp 设置为字段内容.
Select Case _
rst.Fields(lngFldCount).Type
Case 11
strTemp = ""
Case dbText, dbMemo
strTemp = _
rst.Fields(lngFldCount)
Case Else
strTemp = _
str(rst.Fields(lngFldCount))
End Select
End If
Debug.Print Left(strTemp _
& Space(intFldLen), intFldLen);
Next lngFldCount
Debug.Print
rst.MoveNext
Next lngRecCount
End Sub