1

This question has been asked previously. I adjusted my code (below) based on what I understood from research on this topic. However, my code breaks down on line in bold below.

I have a form (PBCIncSum) where the user can input the start and end date criteria for a query RstName. This function is to calculate the 95th percentile values for the data that is in query RstName. When I run the code I get a run-time error '3061: Too few parameters. Expected 2. What am I doing wrong? Spent three days staring at this code without any solution.

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

End Function

1
  • I have a follow-up question. How do I calculate the percentile for only non-NULL or non-ZERO values? Commented Oct 18, 2016 at 19:46

1 Answer 1

1

What am I doing wrong?

You are trying to use CurrentDb.OpenRecordset to run the query. You have created the QueryDef object qdf and defined its parameters, so you need to use the OpenRecordset method of that QueryDef object:

Set rstOrig = qdf.OpenRecordset
Sign up to request clarification or add additional context in comments.

3 Comments

Gord, Thanks for your quick reply. When I do what you suggested, it gives me the following error on the same line: run-time error '3001': Invalid argument. Sunil.
I tried the following code and it worked!!! Thanks for your help!!! :-)
Set rstOrig = qdf.OpenRecordset()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.