Hi all
I am working on an accounting project using MS Access 2000, that demands to calculate many fields in a spesific record.
The problem:
The query I am using returns many records and for each record 3 fields, let's say Data1, Data2, Data3.
In the form related to this query I have 5 text boxes.The three text boxes are bound to the query. The two other text boxes are for displaying calculations,
the first one displays the sum of the three fields (Data1, Data2, Data3), and the other one displays their average.
Note that just the positif numbers (>0) are calculated.
Ex: Data1=10
Data2=0
Data3=20
the result:
textbox4=30 (10+20)
textbox5=15 ((10+20)/2)
What I need is: How can I assing the result of summation and the average to the text boxes for each record?
5 16214
Try this ... -
SELECT Data1, Data2, Data3, (Data1 + Data2 + Data3) As Total,
-
IIf([Data1]=0, IIf([Data2]=0, IIf([Data3]=0,0),1,2),3) As AvgNum,
-
IIf(AvgNum<>0,Total/AvgNum,0) As AvgTotal
-
FROM TableName;
-
Mary
NeoPa 32,556
Expert Mod 16PB
As Mary says, it is better to do the calculation in the Query itself. It is possible, though far more complex, to do it in the Record Source of the TextBoxes.
Try this ... -
SELECT Data1, Data2, Data3, (Data1 + Data2 + Data3) As Total,
-
IIf([Data1]=0, IIf([Data2]=0, IIf([Data3]=0,0),1,2),3) As AvgNum,
-
IIf(AvgNum<>0,Total/AvgNum,0) As AvgTotal
-
FROM TableName;
-
Mary
Thanks Mary for your support but this is not what I need.
First: a syntax error occurs when running the code. I think the problem is in the second line near "1,2)". The ",2" is extra...
Second: in the example I gave I said that Data1=10 and Data2=0 and Data3=20
so the average must be (10+20)/2. Using your code the result is (10+30)/3, witch's wrong.
Third: If the first field "Data1" is <> 0, he is not going to test the second field nor the third.
But thank you because using IIF in query was new to me. So I built my own query using IIF and it worked very well. - Select Data1, Data2, Data3, (Data1+ Data2+ Data3) as Total,
-
IIF (Data1 =0, 0, 1) as D1,
-
IIF (Data2 =0, 0, 1) as D2,
-
IIF (Data3 =0, 0, 1) as D3,
-
(D1+ D2+ D3) as Avg,
-
IIF (Avg= 0, 0, Total/Avg) as Average)
-
From tablename
Thanks Mary for your support but this is not what I need.
First: a syntax error occurs when running the code. I think the problem is in the second line near "1,2)". The ",2" is extra...
Second: in the example I gave I said that Data1=10 and Data2=0 and Data3=20
so the average must be (10+20)/2. Using your code the result is (10+30)/3, witch's wrong.
Third: If the first field "Data1" is <> 0, he is not going to test the second field nor the third.
But thank you because using IIF in query was new to me. So I built my own query using IIF and it worked very well.
Select Data1, Data2, Data3, (Data1+ Data2+ Data3) as Total,
IIF (Data1 =0, 0, 1) as D1,
IIF (Data2 =0, 0, 1) as D2,
IIF (Data3 =0, 0, 1) as D3,
(D1+ D2+ D3) as Avg,
IIF (Avg= 0, 0, Total/Avg) as Average)
From tablename
Glad you got it working. My fault I hadn't tested the IIf statement properly.
Mayr
NeoPa 32,556
Expert Mod 16PB
You can tidy it up a little more as D1 through D3 are not required more than once : - SELECT Data1,Data2,Data3,
-
(Data1+Data2+Data3) as Total,
-
IIf(Data1=0,0,1)+IIf(Data2=0,0,1)+IIf(Data3=0,0,1) As Avg,
-
IIF (Avg=0,0,Total/Avg) as Average)
-
FROM tablename
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sql-db2-dba |
last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula"
for average row size when you have variable length records. Or you
really have to know what your application is packing into...
|
by: Steven Stewart |
last post by:
Hi there,
I have posted about this before, but yet to arrive at a solution. I am
going to try to explain this better.
I have an Employees table and a Datarecords table (one-to-many...
|
by: Wayne Aprato |
last post by:
I've read most, if not all, of the posts on moving average and still
can't find a simple solution to my problem (if a simple solution
exists!) I have a table with 2 fields: Hours and Injuries. I...
|
by: mscdex |
last post by:
I have a server application that accepts file transfers (utilitzing
tcplistener) and was wondering how I would efficiently go about
determining the calculate transfer rate while I am transferring...
|
by: larry |
last post by:
Hi,
Is there a way to calculate the sum of the numbers in each field for
each record in the recordset returned from the query? Do I have to use
VBScript?
Thanks,
Larry
|
by: SuzK |
last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are
Week Ending (date)
ItemNbr (double)
Sales Dollars...
|
by: denveromlp |
last post by:
Hello,
I'm new to Access and trying to calculate a rolling 12 month average from some time data. Each data point is a date and a measurement taken at that date. As far as I can tell, the only...
|
by: FishVal |
last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL.
So, let us say the problem is to calculate...
|
by: Wiredboy |
last post by:
Hi, i'm pretty new in this and i hope somebody can help me with this:
i have a db with 5 columns with different values and i want to calculate the average of them. I've tried to create a formula...
|
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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: 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: 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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |