473,405 Members | 2,282 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,405 software developers and data experts.

Modifying VBA

I have this code I use for median:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.  
  5.  
  6. Function DMedian(tName As String, fldName As String) As Single
  7.   Dim MedianDB As DAO.Database
  8.   Dim ssMedian As DAO.Recordset
  9.   Dim RCount As Integer, i As Integer, x As Double, y As Double, _
  10.       OffSet As Integer
  11.   Set MedianDB = CurrentDb()
  12.   Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
  13.             "] FROM [" & tName & "] WHERE [" & fldName & _
  14.             "] IS NOT NULL ORDER BY [" & fldName & "];")
  15.   'NOTE: To include nulls when calculating the median value, omit
  16.   'WHERE [" & fldName & "] IS NOT NULL from the example.
  17.   ssMedian.MoveLast
  18.   RCount% = ssMedian.RecordCount
  19.   x = RCount Mod 2
  20.   If x <> 0 Then
  21.      OffSet = ((RCount + 1) / 2) - 2
  22.      For i% = 0 To OffSet
  23.         ssMedian.MovePrevious
  24.      Next i
  25.      DMedian = ssMedian(fldName)
  26.   Else
  27.      OffSet = (RCount / 2) - 2
  28.      For i = 0 To OffSet
  29.         ssMedian.MovePrevious
  30.      Next i
  31.      x = ssMedian(fldName)
  32.      ssMedian.MovePrevious
  33.      y = ssMedian(fldName)
  34.      DMedian = (x + y) / 2
  35.   End If
  36.   If Not ssMedian Is Nothing Then
  37.      ssMedian.Close
  38.      Set ssMedian = Nothing
  39.   End If
  40.   Set MedianDB = Nothing
  41. End Function
  42.  
  43.  
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.
Dec 22 '11 #1
9 1636
Rabbit
12,516 Expert Mod 8TB
Check the record count before continuing with the code.
Dec 22 '11 #2
Good Idea.

My debug stops at this: line 17
Expand|Select|Wrap|Line Numbers
  1.  ssMedian.MoveLast
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?

Expand|Select|Wrap|Line Numbers
  1. IIf(RecordCount > 0, rest of code, "NONE") 
Am I close?
Dec 22 '11 #3
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.
Dec 22 '11 #4
ADezii
8,834 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3.  
  4. Set MyDB = CurrentDb
  5. Set rst = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
  6.  
  7. With rst
  8.   If (.BOF And .EOF) Then
  9.     MsgBox "No Records to analyze"
  10.       Exit Sub
  11.   Else
  12.     Do Until .EOF
  13.       Debug.Print ![lastname]
  14.         .MoveNext
  15.     Loop
  16.   End If
  17. End With
  18.  
  19. rst.Close
  20. Set rst = Nothing
Dec 23 '11 #5
So I should add this above line 17?

Expand|Select|Wrap|Line Numbers
  1. With ssMedian 
  2.   If (.BOF And .EOF) Then 
  3.     Set DMedian to "NONE" 
  4.       Exit Sub 
  5.   Else 
  6.     Do Until .EOF 
  7.               .MoveNext 
  8.     Loop 
  9.   End If 
  10. End With 
  11.  
Dec 23 '11 #6
ADezii
8,834 Expert 8TB
It is simply another option for you to use, should you take this approach, that is logically where the Code Segment should be placed.
Dec 23 '11 #7
I added this above line 17 from post 1.

Expand|Select|Wrap|Line Numbers
  1.  With ssMedian
  2.   If (.BOF And .EOF) Then
  3.     DMedian = "NONE"
  4.         End Function
  5.   Else
  6.     Do Until .EOF
  7.               .MoveNext
  8.     Loop
  9.   End If
  10. End With
I get an error of: "Block If without End If"

Any ideas? It looks like the statement is closed.
Dec 27 '11 #8
Stewart Ross
2,545 Expert Mod 2GB
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
Dec 27 '11 #9
Thanks, Stewart! That solved the issue. Thanks also for the data type concern in returning a string. I have decided to go with "0".
Dec 27 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

16
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
15
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...
2
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...
6
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,...
6
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=...
12
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;
1
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,
13
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 =...
56
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 ?
5
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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
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...
0
tracyyun
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...
0
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,...

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.