I have a tricky average calculation using multiple text fields and need to ignore non numeric values but include zero in avg calculation.
If fields are all Text format
Jan = 10
Feb = 0
Mar = 15
Apr = X
May = 15
The avg here is 10+0+15+15/4 = 10
We need to use dsum to add up the IsNumeric values and us DCount to divide by IsNumeric values
It should be doing the following but i cannot get it right. : - Avg_value: ((DSum("[Jan]+[Feb]+[Mar]+[Apr]+May]","Table1","[Jan-May]>=0"))/(DCount("[Jan]+[Feb]+[Mar]+[Apr]+May]","Table1","[Jan-May]>=0")),2)
-
I first tried to eliminate the non-numerics for all the fields (Jan1-May1) Apr will then be blank. I used ie - Jan1: IIf(IsNumeric([quantity1])=True,[quantity1])
And then tried the avg on these 5 fields - Calc: Avg(Nz([jan1])+Nz([feb1])+Nz([mar1]+Nz([apr1])+Nz([may1]),0))
No success as it gives me a funny answer. Please advise
6 1293
You might want to try something like this: - AvgValue: (Val([Jan])+Val([Feb])+Val([Mar])+Val([Apr])+Val([May]))/5
If you need to completely remove non-numerics from the Average, you might want to structure things like this: - SELECT
-
Table1.Jan
-
, Table1.Feb
-
, Table1.Mar
-
, Table1.Apr
-
, Table1.May
-
, IIf([Divisor]>0,(Val([Jan])+Val([Feb])+Val([Mar])+Val([Apr])+Val([May]))/[Divisor],0) AS AvgValue
-
, [IncludeJan]+[IncludeFeb]+[IncludeMar]+[IncludeApr]+[IncludeMay] AS Divisor
-
, Abs(IsNumeric([Jan])) AS IncludeJan
-
, Abs(IsNumeric([Feb])) AS IncludeFeb
-
, Abs(IsNumeric([Mar])) AS IncludeMar
-
, Abs(IsNumeric([Apr])) AS IncludeApr
-
, Abs(IsNumeric([May])) AS IncludeMay
-
FROM Table1
I think this code is less restrictive. It ignores field names and doesn't care how many months you define. -
Option Compare Database
-
Option Explicit
-
-
Function Average() As Currency
-
-
Dim MyDb As Database
-
Dim Rst As Recordset
-
Dim SQLStg As String
-
Dim Fld As Field
-
Dim Tot As Currency
-
Dim i As Integer
-
-
SQLStg = "SELECT Table1.* FROM Table1"
-
-
Set MyDb = CurrentDb
-
Set Rst = MyDb.OpenRecordset(SQLStg)
-
-
With Rst
-
For Each Fld In .Fields
-
If IsNumeric(Fld) Then
-
Tot = Tot + Fld
-
i = i + 1
-
End If
-
Next Fld
-
.Close
-
Set Rst = Nothing
-
End With
-
-
Average = Tot / i
-
-
End Function
-
Phil
Thx I will give it a try. The "X" is added for those months the stock is unobtainable from the supplier, and thats why i need to be able to remove it from the avg calculation, as it will skew the overall averages for the other months. Otherwise , it would be straightforward just to divide by the number of months.
Thx for advice JForbes.I agree thats the way i would have preferred to go, but the about 550 items here are populated onto a screen alphabetically from top to bottom, and the data is filled in from left to right, for ease of capturing as the users battle to find correct items if having to lookup by names.So i am extracting some of this info from left to right for different reports. Thx for prompt reply
JForbes, I got it working with the Sql added to the query. Thx for the trouble. You are a Star!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Roy Adams |
last post by:
Hello people I've recently been woring with multiple insert from text
fields and got that woking fine thanks to the help from people from
this forum
now i'm trying to deal with multiple update,...
|
by: Paul Morrow |
last post by:
I have seen the technique where a number of rows in a database are
displayed in an html table so that each column of each row is
editable. They use a single form surrounding the table, where each...
|
by: wolfing1 |
last post by:
Like, I know if several checkboxes have the same name, I can get the
ones that were checked after submit with a request.form("name") and
maybe do a split(request.form("name"),",") to cycle through...
|
by: TORQUE |
last post by:
Hi,
Im wondering if anyone can help me with a problem.
I have a form with more than 50 unbound fields.
Some of the fields will be blank from time to time.
This seems to be where im having...
|
by: letam |
last post by:
Hello, Thank you in advance for any help, it will be most appreciated!
I need to be able to disable several text fields, until the user enters a value in a certain text field. Once they enter a...
|
by: Dan |
last post by:
Hi all, I am creating a search table where the keywords field is made
up of several text fields and this is causing me some problems. I can
concatentate the text ok but i can't seem to concatenate...
|
by: jw01 |
last post by:
Hi,
I have created a text widget with multiple text fields using Perl Tk. So its like a web form e.g
Name: (user entered xyz)
Address:(user entered 123)
Age:(user entered 77)
When a user...
|
by: sesling |
last post by:
We have several tables that store numeric data as well as text fields that we receive from our customers. Some customers use " in the text fields. When I query the data and export the results to a...
|
by: chandhseke |
last post by:
Hi Team,
Could you please educate me with an explanation - What is the impact of giving same name for different text fields in HTML/ASP. I use JavaScript for Client side validation and few other...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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: 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...
|
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: 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...
|
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,...
| |