Sql Microsoft Office Access-中值函数-参数太少

Sql Microsoft Office Access-中值函数-参数太少,sql,vba,ms-access,parameters,median,Sql,Vba,Ms Access,Parameters,Median,我正试图使用此代码从我的查询中计算中位数,该查询具有以下条件: [Form]![testForm2]![crit2]和[Form]![testForm2]![Age2] 如果没有这些标准,函数可以很好地工作,并根据“MP”给出每个任务的中位数,但是当我将标准放入其中时,我收到错误: 错误-参数太少。预期为4,然后显示“对象变量或未设置块” 我的输入:DMedian(“MP”;“testForm2”;“[TASK]=”&[TASK]&“””) *即使在表单打开时,也会出现错误。 *我可能需要找到一

我正试图使用此代码从我的查询中计算中位数,该查询具有以下条件:
[Form]![testForm2]![crit2]和[Form]![testForm2]![Age2]

如果没有这些标准,函数可以很好地工作,并根据“MP”给出每个任务的中位数,但是当我将标准放入其中时,我收到错误: 错误-参数太少。预期为4,然后显示“对象变量或未设置块”

我的输入:
DMedian(“MP”;“testForm2”;“[TASK]=”&[TASK]&“””)

*即使在表单打开时,也会出现错误。 *我可能需要找到一种不同的方法从表单中过滤这个查询,但我不知道如何过滤

Public Function DMedian(FieldName As String, _
      TableName As String, _
      Optional Criteria As Variant) As Variant

' Created by Roger J. Carlson
' http://www.rogersaccesslibrary.com
' Terms of use: You may use this function in any application, but
' it must include this notice.

'Returns the median of a given field in a given table.
'Returns -1 if no recordset is created

' You use this function much like the built-in Domain functions
' (DLookUp, DMax, and so on). That is, you must provide the
' 1) field name, 2) table name, and 3) a 'Where' Criteria.
' When used in an aggregate query, you MUST add each field
' in the GROUP BY clause into the into the Where Criteria
' of this function.

' See Help for more on Domain Aggregate functions.

On Error GoTo Err_Median

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim RowCount As Long
    Dim LowMedian As Double, HighMedian As Double

    'Open a recordset on the table.
    Set db = CurrentDb
    strSQL = "SELECT " & FieldName & " FROM " & TableName
    If Not IsMissing(Criteria) Then
        strSQL = strSQL & " WHERE " & Criteria & " ORDER BY " & FieldName
    Else
        strSQL = strSQL & " ORDER BY " & FieldName
    End If
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

    'Find the number of rows in the table.
    rs.MoveLast
    RowCount = rs.RecordCount
    rs.MoveFirst

    'Determine Even or Odd
    If RowCount Mod 2 = 0 Then
        'There is an even number of records. Determine the low and high
        'values in the middle and average them.
        rs.Move Int(RowCount / 2) - 1
        LowMedian = rs(FieldName)
        rs.Move 1
        HighMedian = rs(FieldName)
        'Return Median
        DMedian = (LowMedian + HighMedian) / 2
    Else
        'There is an odd number of records. Return the value exactly in
        'the middle.
        rs.Move Int(RowCount / 2)
        'Return Median
        DMedian = rs(FieldName)
    End If

Exit_Median:
    'close recordset
    rs.Close
    Exit Function

Err_Median:
    If Err.number = 3075 Then
        DMedian = 0
        Resume Exit_Median
    ElseIf Err.number = 3021 Then
        'EOF or BOF ie no recordset created
        DMedian = -1
        Resume Exit_Median
    Else
        MsgBox Err.Description
        Resume Exit_Median
    End If
End Function

参数分隔字符为逗号,使用分号

更改:

DMedian("MP";"testForm2";"[TASK]= '" & [TASK] & "'")
致:


解决方案是引用SQL声明中的文本框,谢谢大家

像这样:

HAVING (((Data.[REV]< " & Me.crit1 & ") And (Data.[REV])>" & Me.crit2 & ") AND ((Reg.Age)<" & Me.Age1 & " And (Reg.Age)>" & Me.Age2 & " " & SQLcritComplete & "));"
"HAVING (((Data.[REV]<[Form]![testForm2]![crit1]) And (Data.[REV])>[testForm2]![crit2]) AND ((Reg.Age)<[Form]![testForm2]![Age1] And (Reg.Age)>[Form]![testForm2]![Age2] & SQLcritComplete & "));"
具有(((数据。[REV]<“&Me.crit1&”)和((数据。[REV])>”&Me.crit2&”)和((注册年龄)&Me.Age2&“&SQLcritComplete&”);"
不是这样的:

HAVING (((Data.[REV]< " & Me.crit1 & ") And (Data.[REV])>" & Me.crit2 & ") AND ((Reg.Age)<" & Me.Age1 & " And (Reg.Age)>" & Me.Age2 & " " & SQLcritComplete & "));"
"HAVING (((Data.[REV]<[Form]![testForm2]![crit1]) And (Data.[REV])>[testForm2]![crit2]) AND ((Reg.Age)<[Form]![testForm2]![Age1] And (Reg.Age)>[Form]![testForm2]![Age2] & SQLcritComplete & "));"
“具有(((数据。[REV][testForm2]![crit2])和((注册年龄)[Form]![testForm2]![Age2]![SQLcritComplete&')

您要将哪些参数传递给函数?哪一行失败?无法使用动态参数化查询。条件必须包含在函数参数中。我不使用动态参数化查询。我对表单或报表应用筛选器。“进一步计算”是什么意思?如果您希望DMedian聚合这4个p选择的相同记录参数然后您需要在函数调用中具有相同的4个参数。对表单使用动态参数化查询(或将筛选条件应用于表单/报表)然后使用函数引用表并使用所有必要的条件执行。函数使用的条件也可以引用表单控件。实际上,应该能够在动态参数化查询中包含函数调用。同样,函数调用必须引用表并包含所有必要的条件。抱歉,不,这不是问题所在m、 至少在我的Access版本中,分隔符是分号。如果没有我表单中的条件,此函数运行得很好:
[form]![testForm2]![crit2]和[form]![testForm2]![Age2]
,但我确实需要它来处理基于该表单的条件。非美国Access使用分号分隔符。