# Use public function in MS Access query

I am trying to calculate percentile using a MS Access query.
and I am trying to use a Access query that calls a function.

For example, I have a table and with field A, B, and C.  I need to first group the table by A and B, then within each group I need to calculate percentile for C.

I understand I can write a function using VBA but then the function works only for the whole column of C. The function would need input of table name, field name, and percentile.  Can anybody please show me how to apply the function for each group (by A and B) instead of just the whole field?

###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MS Access Systems CreatorCommented:
Hey mugua!

a percentile require something to compare against:  x/y = z%
With the total in Group A/B being x,..
Where will the y value come from?

regards
Jack
Commented:
if you want to use in a query, you will calculate each record

something like      DoPerc(A,B,C)  - you have this value at hand in your query

Public Function DoPerc( a_val As Double, b_val As Double c_val As Double) As Double
Dim sqlstr As String
Dim rs As Recordset
sqlstr = "SELECT >some Agregatfunction with A.basetab, B.basetab< FROM basetab GROUP BY A.basetab, B.basetab;"

now do your calculation with rs.Fields(0),rs.Fields(1) & c_val

DoPerc = ................
End Function

Depending on the amount of data, it would be less calculating doing a straight forward double loop, maybe to a temp table ?

regards, franz

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Did the above solution(s) fix your problem?
Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: bonjour-aut {http:#9617723}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

stevbe
EE Cleanup Volunteer
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.