I have this code I use for median: - Option Compare Database
-
Option Explicit
-
-
-
-
Function DMedian(tName As String, fldName As String) As Single
-
Dim MedianDB As DAO.Database
-
Dim ssMedian As DAO.Recordset
-
Dim RCount As Integer, i As Integer, x As Double, y As Double, _
-
OffSet As Integer
-
Set MedianDB = CurrentDb()
-
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
-
"] FROM [" & tName & "] WHERE [" & fldName & _
-
"] IS NOT NULL ORDER BY [" & fldName & "];")
-
'NOTE: To include nulls when calculating the median value, omit
-
'WHERE [" & fldName & "] IS NOT NULL from the example.
-
ssMedian.MoveLast
-
RCount% = ssMedian.RecordCount
-
x = RCount Mod 2
-
If x <> 0 Then
-
OffSet = ((RCount + 1) / 2) - 2
-
For i% = 0 To OffSet
-
ssMedian.MovePrevious
-
Next i
-
DMedian = ssMedian(fldName)
-
Else
-
OffSet = (RCount / 2) - 2
-
For i = 0 To OffSet
-
ssMedian.MovePrevious
-
Next i
-
x = ssMedian(fldName)
-
ssMedian.MovePrevious
-
y = ssMedian(fldName)
-
DMedian = (x + y) / 2
-
End If
-
If Not ssMedian Is Nothing Then
-
ssMedian.Close
-
Set ssMedian = Nothing
-
End If
-
Set MedianDB = Nothing
-
End Function
-
-
I was wondering how I could add a statement that will check to see if there are records to compute and if not simply return the string "NONE". Currently the code will have runtime errors is there are no values to calculate.
9 1636
Check the record count before continuing with the code.
Good Idea.
My debug stops at this: line 17
I'm by no means a pro at programming. My guess is that I will insert an IIf statement above that line of code. Would it be like this? - IIf(RecordCount > 0, rest of code, "NONE")
Am I close?
NeoPa 32,556
Expert Mod 16PB Wisni1rr:
Am I close?
Very.
Unfortunately, there are situations where RecordCount is not set correctly immediately after the Recordset is opened, so test using EOF instead.
As a side note, you do not need RecordCount, and you do not need to traverse a Recordset to get an accurate count. A more suble way is to test and see if the EOF and BOF a Recordset both evaluate to True, in which case the Recordset will definitely contain Records. A simple example will illustrate this point: - Dim MyDB As DAO.Database
-
Dim rst As DAO.Recordset
-
-
Set MyDB = CurrentDb
-
Set rst = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
-
-
With rst
-
If (.BOF And .EOF) Then
-
MsgBox "No Records to analyze"
-
Exit Sub
-
Else
-
Do Until .EOF
-
Debug.Print ![lastname]
-
.MoveNext
-
Loop
-
End If
-
End With
-
-
rst.Close
-
Set rst = Nothing
So I should add this above line 17? - With ssMedian
-
If (.BOF And .EOF) Then
-
Set DMedian to "NONE"
-
Exit Sub
-
Else
-
Do Until .EOF
-
.MoveNext
-
Loop
-
End If
-
End With
-
It is simply another option for you to use, should you take this approach, that is logically where the Code Segment should be placed.
I added this above line 17 from post 1. - With ssMedian
-
If (.BOF And .EOF) Then
-
DMedian = "NONE"
-
End Function
-
Else
-
Do Until .EOF
-
.MoveNext
-
Loop
-
End If
-
End With
I get an error of: "Block If without End If"
Any ideas? It looks like the statement is closed.
Line 4 above should read Exit Function not
End Function
Please note also that in line 3 you cannot return a text value - the function is defined as returning a single-precision number. You would need to return a 0 or some such 'safe' value consistent with the function's defined type.
-Stewart
Thanks, Stewart! That solved the issue. Thanks also for the data type concern in returning a string. I have decided to go with "0".
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Japcuh |
last post by:
How do you write self modifying code in Java?
Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
..0.
...0
000
|
by: Paul Paterson |
last post by:
I am trying to find a way to mimic by-reference argument passing for
immutables in Python. I need to do this because I am writing an
automated VB to Python converter.
Here's an example of the VB...
|
by: Jean-S?bastien Bolduc |
last post by:
I'm afraid this is a silly question, to which I know the answer
already. But let me ask anyway...
In Python, is there a way to modify a builtin type's methods? for
instance, modifying...
|
by: Peter Ballard |
last post by:
Whew. I hope that title is descriptive!
Hi all,
The python tutorial tells me "It is not safe to modify the sequence
being iterated over in the loop". But what if my list elements are
mutable,...
|
by: qwweeeit |
last post by:
Hi all,
when I was young I programmed in an interpreted language that allowed
to modify itself.
Also Python can (writing and running a module, in-line):
fNew =open("newModule.py",'w')
lNew=...
|
by: pvinodhkumar |
last post by:
1)
char* p = "Plato";
p = 'r'; // runtime error
2)
char c = "Plato";
c = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;
|
by: A.M-SG |
last post by:
Hi,
Can I view/modify SOAP message at the client proxy side without using soap
extensions?
Thank you,
|
by: Robin Becker |
last post by:
When young I was warned repeatedly by more knowledgeable folk that self
modifying code was dangerous.
Is the following idiom dangerous or unpythonic?
def func(a):
global func, data
data =...
|
by: subramanian100in |
last post by:
The standard allows that we can copy strings onto arg, arg, etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?
|
by: IUnknown |
last post by:
Ok, we are all aware of the situation where modifying the folder
structure (adding files, folders, deleting files, etc) will result in
ASP.NET triggering a recompilation/restart of the application....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |