この質問は前に要求されました。私はこのトピックに関する研究から理解したことに基づいてコードを調整しました(下記)。しかし、私のコードは下の太字の行に分かれています。クエリへのパラメータの受け渡しAccess VBA
私はRstName
というクエリの開始日と終了日の基準をユーザが入力できる形式(PBCIncSum
)を持っています。この関数は、クエリRstName
にあるデータの95パーセンタイル値を計算します。コードを実行すると、実行時エラー '3061:パラメータが少なすぎます。期待している2.何が間違っているの?解決せずにこのコードを見つめて3日を費やした。
Public Function PercentileRst(RstName As String, fldName As String, PercentileValue As Double) As Double
'This function will calculate the percentile of a recordset.
'The field must be a number value and the percentile has to
'be between 0 and 1.
If PercentileValue < 0 Or PercentileValue > 1 Then
MsgBox "Percentile must be between 0 and 1", vbOKOnly
End If
Dim PercentileTemp As Double
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim xVal As Double
Dim iRec As Long
Dim i As Long
Dim RstOrig As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set qdf = dbs.QueryDefs(RstName)
qdf.Parameters(0) = Forms!PBCIncSum!StDate
qdf.Parameters(1) = Forms!PBCIncSum!EndDate
For Each prm In qdf.Parameters
prm = Eval(prm.Name)
Next prm
**Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)**
RstOrig.Sort = fldName
Dim RstSorted As DAO.Recordset
Set RstSorted = RstOrig.OpenRecordset()
RstSorted.MoveLast
RstSorted.MoveFirst
xVal = ((RstSorted.RecordCount - 1) * PercentileValue) + 1
'x now contains the record number we are looking for.
'Note x may not be whole number
iRec = Int(xVal)
xVal = xVal - iRec
'i now contains first record to look at and
'x contains diff to next record
RstSorted.Move iRec - 1
PercentileTemp = RstSorted(fldName)
If xVal > 0 Then
RstSorted.MoveNext
PercentileTemp = ((RstSorted(fldName) - PercentileTemp) * xVal) + PercentileTemp
End If
RstSorted.Close
RstOrig.Close
Set RstSorted = Nothing
Set RstOrig = Nothing
Set dbs = Nothing
Set qdf = Nothing
PercentileRst = PercentileTemp
エンド機能
私がフォローアップの質問を持っている:あなたは、そのクエリ定義オブジェクトの
OpenRecordset
メソッドを使用する必要があるので、クエリ定義オブジェクトqdf
を作成し、そのパラメータを定義しています。非NULL値またはゼロ以外の値のパーセンタイルはどのようにして計算できますか? –