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

Calculate the average from multiple text fields and ignore if value is not numeric

547 512MB
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. :
Expand|Select|Wrap|Line Numbers
  1. Avg_value: ((DSum("[Jan]+[Feb]+[Mar]+[Apr]+May]","Table1","[Jan-May]>=0"))/(DCount("[Jan]+[Feb]+[Mar]+[Apr]+May]","Table1","[Jan-May]>=0")),2)
  2.  
I first tried to eliminate the non-numerics for all the fields (Jan1-May1) Apr will then be blank. I used ie
Expand|Select|Wrap|Line Numbers
  1. Jan1: IIf(IsNumeric([quantity1])=True,[quantity1])
And then tried the avg on these 5 fields
Expand|Select|Wrap|Line Numbers
  1. Calc: Avg(Nz([jan1])+Nz([feb1])+Nz([mar1]+Nz([apr1])+Nz([may1]),0))
No success as it gives me a funny answer. Please advise
Attached Files
File Type: zip MonthlyAverage.zip (61.5 KB, 68 views)
Nov 30 '16 #1
6 1293
jforbes
1,107 Expert 1GB
You might want to try something like this:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   Table1.Jan
  3. , Table1.Feb
  4. , Table1.Mar
  5. , Table1.Apr
  6. , Table1.May
  7. , IIf([Divisor]>0,(Val([Jan])+Val([Feb])+Val([Mar])+Val([Apr])+Val([May]))/[Divisor],0) AS AvgValue
  8. , [IncludeJan]+[IncludeFeb]+[IncludeMar]+[IncludeApr]+[IncludeMay] AS Divisor
  9. , Abs(IsNumeric([Jan])) AS IncludeJan
  10. , Abs(IsNumeric([Feb])) AS IncludeFeb
  11. , Abs(IsNumeric([Mar])) AS IncludeMar
  12. , Abs(IsNumeric([Apr])) AS IncludeApr
  13. , Abs(IsNumeric([May])) AS IncludeMay
  14. FROM Table1
Nov 30 '16 #2
PhilOfWalton
1,430 Expert 1GB
I think this code is less restrictive. It ignores field names and doesn't care how many months you define.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Function Average() As Currency
  5.  
  6.     Dim MyDb As Database
  7.     Dim Rst As Recordset
  8.     Dim SQLStg As String
  9.     Dim Fld As Field
  10.     Dim Tot As Currency
  11.     Dim i As Integer
  12.  
  13.     SQLStg = "SELECT Table1.* FROM Table1"
  14.  
  15.     Set MyDb = CurrentDb
  16.     Set Rst = MyDb.OpenRecordset(SQLStg)
  17.  
  18.     With Rst
  19.         For Each Fld In .Fields
  20.             If IsNumeric(Fld) Then
  21.                 Tot = Tot + Fld
  22.                 i = i + 1
  23.             End If
  24.         Next Fld
  25.         .Close
  26.         Set Rst = Nothing
  27.     End With
  28.  
  29.     Average = Tot / i
  30.  
  31. End Function
  32.  
Phil
Nov 30 '16 #3
neelsfer
547 512MB
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.
Nov 30 '16 #4
jforbes
1,107 Expert 1GB
This problem would go away if you were to normalize your data: Database Normalization and Table Structures

If you were to make a Table with Date column for the Month and Numeric column for the Quantity, you could write a standard Query and everything will work as expected. I modified you sample database and attached it: https://bytes.com/attachment.php?att...1&d=1480530687
Attached Files
File Type: zip MonthlyAverage.zip (56.0 KB, 61 views)
Nov 30 '16 #5
neelsfer
547 512MB
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
Nov 30 '16 #6
neelsfer
547 512MB
JForbes, I got it working with the Sql added to the query. Thx for the trouble. You are a Star!
Nov 30 '16 #7

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

Similar topics

1
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,...
9
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...
17
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...
18
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...
2
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...
4
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...
11
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...
1
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...
13
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...
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: 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
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
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
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
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...
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.