1

我想将 Access 2010 中单个字段的所有记录相乘。我尝试过mult(Field name)product(field name)无济于事。任何人都可以帮助我 Access 中是否有任何功能可以这样做?

示例:
我有一个包含字段 S1 的表

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

并且输出应该在另一个具有字段结果的表中

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

1 回答 1

3

不幸的是,PRODUCT()Access 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
于 2013-10-28T09:22:40.853 回答