473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Median function in Access calling Excel worksheetfunction, or change to any excel function on a query recordset

Here's a median function in access that you can call in any query. You
could change this to any excel function you wanted. Most people can
find the windows help file that says how to call an excel function but
don't know how to pass an array of the recordset they made into that
function. This uses GetRows that nicely creates an array that can be
passed into excel.

I have this data table for testing. The table name is TestData

Names Numbers
Charles 3
Henry 2.5
Henry 2
Henry 3
Henry 1
John 63
Ken 6
Ken 45
Ken 13
Lewis 5
Opi 9
Opi 45
Opi 9
Opi 36
Paul 1
Call in a query MyFunc: Median("TestData","Numbers","Names"," ='Ken' ")
Public Function Median(Table As String, TgtField As String, LimField As
Variant, Criteria As Variant) As Double

Dim mySQL As String
Dim rst As Recordset
Dim N As Long ' number of records

On Error GoTo 10000
If IsNull(LimField) Or IsNull(Criteria) Then
mySQL = "SELECT " & TgtField & " FROM " & Table & ";"
Else
mySQL = "SELECT " & TgtField & " FROM " & Table & " WHERE [" &
LimField & "] " & Criteria & "" & ";"
End If

Set rst = CurrentDb.OpenRecordset(mySQL, dbOpenDynaset)
rst.MoveLast 'you need this to force access to actually load
all of them.
N = rst.RecordCount

rst.MoveFirst 'getrows defaults to getting rows from your current
position (bookmark) to number of rows you specify (N) in our case which
is the total number of records.
Median = Excel.Application.Median(rst.GetRows(N)) 'You have to have
a reference to Microsoft Excel in your references in tools..references

rst.Close
GoTo 20000

10000:
MsgBox "Error in-----" & mySQL

'other way of using excel
'Dim obj As Excel.Application
' Set obj = CreateObject("Excel.Application")
' MsgBox obj.Application.Median(1, 2, 5, 8, 12, 13)
' obj.Quit
' Set obj = Nothing

20000:
End Function


If you like this send me an e-mail. I want to be famous.

Jul 26 '06 #1
1 6471
If you like this send me an e-mail. I want to be famous.

For what? There's been code for this on MS's website for YEARS. Oh
well, so much for being famous.

Jul 27 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
3
by: gunnarnime | last post by:
Can I get the following function to work with Fieldnames that have spaces in it? I've tried spqaure brackets to no avail. Any help would be appreciated. Public Function DMedian(FieldName As...
1
by: jobxyz | last post by:
Hi I am trying to call the excel function linest from VB using automation. the call Slope1 = Excel.WorksheetFunction.Index(Excel.WorksheetFunction.LinEst(y, x),1) works fine as long as x...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
3
by: Scott | last post by:
I need to take the median from a field of records in a report. Can someone shed the light how to do it. Thanks, Scott
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.