473,847 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Median function in Access calling Excel worksheetfuncti on, 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("TestDat a","Numbers","N ames"," ='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.OpenR ecordset(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.Applicati on.Median(rst.G etRows(N)) 'You have to have
a reference to Microsoft Excel in your references in tools..referenc es

rst.Close
GoTo 20000

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

'other way of using excel
'Dim obj As Excel.Applicati on
' Set obj = CreateObject("E xcel.Applicatio n")
' 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 6510
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
2530
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 Northwind no problem. I am having some trouble though converting it to my specific purpose which may be less complicated than the solution Bob (raskew) provided in the thread. Here are my specifics:
8
14116
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 the median of the another, though I'm not sure how. I've been able to add the "Median" function (from the Microsoft Access Help Archive), but I can't figure out how to incorporate that into the Totals. Do I need to use the "expression" and...
3
2952
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 String, _ TableName As String, _ Optional Criteria As Variant) As Double On Error GoTo Err_DMedian
1
6336
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 is a one dimensional array.
4
8181
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
9439
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 query it without making a table out of query 1. I can't find a median function in the "Total" field, so is there so way to make an expression to calculate the median of the orignial data from query 1 in my new query? Also, what does name by...
3
4676
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
5198
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 Storage_Click() On Error GoTo Err_Storage_Click
6
4104
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 median of a recordset. The field must be a number value. Dim MedianTemp As Double Dim RstOrig As Recordset Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10653
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5725
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4129
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.