如何在Access 2010中乘以单个字段的所有记录

时间:2013-10-28 07:03:34

标签: sql ms-access ms-access-2010

我希望将Access 2010中单个字段的所有记录相乘。 我尝试了mult(Field name)product(field name)无济于事。 任何人都可以帮助我在Access中有任何功能吗?

例:
我有一个具有字段S1的表

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

并且输出应该在具有字段结果

的另一个表中
Result
--------
6.25E+24

1 个答案:

答案 0 :(得分:3)

不幸的是,Access SQL中没有PRODUCT()函数可以让你做

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