vba中的DSum函数

时间:2012-06-22 13:44:20

标签: ms-access access-vba

我想在按下按钮时显示文本框中列的总和。但它给了我一个编译错误:“错误的争论数量或无效的财产分配”

以下代码在vba中实现。

以下是我使用的代码:

Text19 = Nz(DSum("Total_Units", "6_Provincial_SUB", , "[BudgetYear] =" & [Combo5] & " And [Program_Name] ='" & Replace([Combo7], "'", "''") & "'"), 0)

2 个答案:

答案 0 :(得分:2)

DSum有三个参数。你有四个。删除额外的逗号

Text19 = Nz(
    DSum(
        "Total_Units",
        "6_Provincial_SUB", <==== Here I dropped a comma (,)
        "[BudgetYear] =" & [Combo5] & " And [Program_Name] ='" &
            Replace([Combo7], "'", "''") & "'"
    ),
    0
)

当这样的事情发生时,我试图通过缩进上面的表达式来找到问题,以便找到匹配的括号等。没有行连续字符“_”这不起作用,但它让你了解表达式的结构。


我的库中有这些功能。它们帮助我创建SQL字符串

Public Function SqlStr(ByVal s As String) As String
'Input: s=""      Returns: NULL
'Input: s="abc"   Returns: 'abc'
'Input: s="x'y"   Returns: 'x''y'

    If s = "" Then
        SqlStr = "NULL"
    Else
        SqlStr = "'" & Replace(s, "'", "''") & "'"
    End If
End Function

Function Build(ByVal s As String, ParamArray args()) As String
'Build("FirstName = {0}, LastName = {1}","John","Doe") --> 
'"FirstName = John, LastName = Doe".
'"\n" is expanded with vbCrLf.

    Dim i As Long

    s = Replace(s, "\n", vbCrLf)
    For i = 0 To UBound(args)
        s = Replace(s, "{" & i & "}", Nz(args(i)))
    Next i
    Build = s
End Function

通过使用它们,您的SQL将像这样构建

sql = Build("[BudgetYear] = {0} AND [Program_Name] = {1}", _
            Combo5, SqlStr(Combo7))

答案 1 :(得分:0)

域参数后面有太多逗号:

  "6_Provincial_SUB", ,

这会使空间成为标准参数,而实际标准则是未知的第四个参数。