如何乘单场的所有记录在Access 2010(How to multiply all records

2019-10-18 20:17发布

我想乘在Access 2010中单场的所有记录,我试图mult(Field name)product(field name)无济于事。 谁能帮我是有在访问任何功能,这样做呢?

例:
我已经有外地S1表

S1 
---
557
560
563
566
569
572
575
578
581

和输出应该是在具有域结果的另一个表

Result
--------
6.25E+24

Answer 1:

不幸的是,没有任何PRODUCT()在访问SQL功能,将允许你这样做

SELECT PRODUCT([S1]) AS Result FROM [YourTable]

但是,您可以使用VBA“滚你自己的” DProduct()域聚合功能,类似于内置DSum()函数:

Option Compare Database
Option Explicit

Public Function DProduct(Expr As String, Domain As String, Optional criteria) As Variant
    Dim SQL As String, Result As Double
    Dim cdb As DAO.Database, rst As DAO.Recordset
    On Error GoTo DProduct_Error
    Set cdb = CurrentDb
    SQL = "SELECT " & Expr & " AS Expr1 FROM [" & Domain & "]"
    If Not IsMissing(criteria) Then
        SQL = SQL & " WHERE " & criteria
    End If
    Set rst = cdb.OpenRecordset(SQL, dbOpenSnapshot)
    If rst.BOF And rst.EOF Then
        DProduct = Null
    Else
        Result = 1
        Do Until rst.EOF
            Result = Result * rst!Expr1
            rst.MoveNext
        Loop
        DProduct = Result
    End If
    rst.Close
    Set rst = Nothing
    Set cdb = Nothing
    Exit Function

DProduct_Error:
    DProduct = Null
End Function

在你的问题的样本数据测试

?DProduct("S1", "YourTable")
 6.24666417941851E+24


文章来源: How to multiply all records of a single field in Access 2010