0

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

下面的代码是在 vba 中实现的。

这是我使用的代码:

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

2 回答 2

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))
于 2012-06-22T13:50:20.863 回答
0

域参数后的逗号太多:

  "6_Provincial_SUB", ,

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

于 2012-06-22T13:50:05.787 回答