Hi,
I have 5 fields on a form. Field1, Field2, Field3, Field4, SumField
SumField is disabled as it is calculated field based on other four fields.
SumField = Field1 + Field2 + Field3 + Field4
It should only sum the values if they are not null. If one of the fields is null it should skip that field.
Also, null field should stay null, the code must not convert it to zero.
Example:
Field1 = 5
Field2 = null
Field3 = null
Field4 = 10
SumField = 15
I know I can do it with IF code for each field, but there is a lot of this fields and it would complicate whole thing.
Any help appreciated.
12 13149 - SumField = nz(Field1;0) + nz(Field2;0) + nz(Field3,0) + nz(Field4,0)
Nz() will check if the field is null, and if so return the second argument (in this case 0). IF field is not null it will return the field value. Think of Nz as a filter to catch your nulls.
BTW. I've just tried to use nz() function, but it gives me run-time error 13 (Type mismatch).
The code is fixed so that after the update of Field1, Field2, Field3 and Field 4 - Calculating sub is called, where the formula is.
Yeah, I've tried to use nz(), but it gives error, see my previous reply.
I've only entered value in Field1 (5).
When I go to debug, I see whole line highlighted yellow with this values:
SumField = ""
Field1 = 5
Field2 = ""
Field3 = ""
Field4 = ""
Are fields 1 to 4 bound or unbound? and if bound, what type field are they bound to? Text, number or something else?
Please post the code your using in full. I would probably just put the - = nz(Field1;0) + nz(Field2;0) + nz(Field3,0) + nz(Field4,0)
-
as the controlsource for sumField, without code.
The fields are unbound.
I've tried to put it in controlsource like you said,
it gives me "#error" in calculated field if some of the fields are null.
If all the fields are filled with number, it calculates correctly.
I think that the problem is that it doesn't recognize other fields as null.
I've used this code, notice the commas: - = nz(Field1;0) + nz(Field2;0) + nz(Field3;0) + nz(Field4;0)
----
In the VBA, the code of the sub: -
Public Sub CalculateFields()
-
-
Form_frmMain.txtSumField = Nz(Form_frmMain.txtField1,0) + Nz(Form_frmMain.txtField2,0) + Nz(Form_frmMain.txtField3,0) + Nz(Form_frmMain.txtField4,0)
-
-
End Sub
Update:
I'm using access 2003 mdb format.
I've created a new accdb database and did a little test, both variants worked perfectly. (VBA and controlsource)
So, I don't know. I know it shouldn't matter, but somehow it does...
NeoPa 32,563
Recognized Expert Moderator MVP
Let's clarify a bit first. We're not talking about fields here at all are we? These are controls on a form which are not related in any way to fields. Please confirm this statement.
As for Nz(), I believe Smiley included a typo in his usage where he used the semi-colon (;) instead of the comma (,). The comma is required.
Unless I'm missing something, there is no cause for a separate procedure to calculate the value as the calculation is done in the .ControlSource of the SumField control.
The values of unbound controls can never be empty strings (I'm not even sure they would be for a control bound to a text field set up as allowing empty strings but not nulls).
For documentary purposes the .ControlSource of the SumField control should be : - =Nz([Field1],0)+Nz([Field2],0)+Nz([Field3],0)+Nz([Field4],0)
NeoPa 32,563
Recognized Expert Moderator MVP
It appears you may need to force this calculation to work as a numeric one by using Val() on the first part. Otherwize it may not realise the value is numeric and treat the whole thing as a string.
Try instead : - =Val(Nz([Field1],0))+Nz([Field2],0)+Nz([Field3],0)+Nz([Field4],0)
PS. This was uncovered by Smiley doing some extra testing on your behalf (See Concatenate (?) versus sum (+) clarification) :)
I've found the problem.
It wasn't the val(), I mean I've noticed this concatenating before, but I've solved it by defining the text control as numeric number in control properties.
I think that the problem was in the clearing procedure which would set the text controls to "" instead of to a NULL.
I've changed that and now it appears to work.
Anyway, thank you guys.
BTW. Control source requires ";" as a separator for nz() function, while VBA requires ",". I'm using Access 2007 and mdb format, but I guess it should be the same for Access 2003 or for the accdb format.
P.S. SmileyOne, lijepi pozdrav! :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Targa |
last post by:
<input NAME="TAXRATE" onBlur="this.form.TAX.value =
(this.form.TAXRATE.value - 0) * (this.form.ITEM1TOTAL.value - 0) +
(this.form.ITEM2TOTAL.value - 0) " Size="4">
In my TAX field I get something like 1.7500000000000002.
Is it possible to format this to read 1.75?
Also, when summing or multiplying fields, Id like to have 4 x 2.00 = 8.00...
|
by: C L Humphreys |
last post by:
Hi,
When are string fields null, and when are they empty?
For example, if the record has never been edited, is it null?
If the record has been edited but deleted, is it an empty string?
I'm not sure whether to use:
x is not null
or
|
by: John Baker |
last post by:
Hi:
I have a situation where I am appending records to a table, and some of the records do NOT
have all the fields in them that the table has. In those cases, the field is filled with a
null but the normal append process. Does anyone know of an easy way to force the use of a
0 (zero) rather than a null when fields don't match on an append?...
|
by: RiesbeckP |
last post by:
Hi All,
I have a DB where there are customer numbers and a few other fields. I
want to be able to pull all of the null records for a particular field
as well as all the other customer numbers that have null and non null
fields (see below).
CustNo Funding ClaimAmt Prod
123 R 100 PPO
123 R 200 ...
|
by: Dan |
last post by:
I am linking two queries in Access. One has data in several fields
such as: Name, Year, Amount, CumTotal1, CumTotal2 where the cumulative
total fields are two subqueries generating cumulative totals based on
the Name, Year, and Amount (e.g. (select sum() from Query1 as
q1 where q1.name = query1.name and q1.year <= query1.year)). When I
run...
| |
by: Mark A |
last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP
11).
Using the SAMPLE database, tables EMP and EMLOYEE.
In the followng stored procedure, 2 NULL columns (COMM) are selected into 2
different SP variables and compared for equal. They are both NULL, but do
not compare as equal. When the Not NULL columns (SALARY)...
|
by: kbeana |
last post by:
I can't figure this out for the life of me. I have several fields that are set up as currency, and all have a decimal place of 2. I am summing some of them in the report footer, and it will be a couple cents off every time. The syntax I'm using is =Sum()
I read another question similar to this one and the answer was that it was only rounding...
|
by: kabradley |
last post by:
Hello all!
I have a logic problem that I have been working on for quite some time now and can't seem to fix. Hopefully someone can shed some light on the subject.
I am starting to work on an access database that will hold stock data. The database needs to be able to do the following:
Compute the correct amount of stock share dividends that...
|
by: Will |
last post by:
Hi,
I need to handle blank values in a query calculation. I have 636
records at the moment but when I sum over these records the blank
fields are not returned. I have looked around here and on Access help
and the Nz function seems to be the way forward.
I am summing over 6 fields, all or none of which may contain values.
The expression...
|
by: samuel84 |
last post by:
Hi,
I am getting the error "invalid use of null" when summing value from a range which has no value and "data type mismatch in criteria expression" when there are values to be summed up. Is there an error in my code? could someone please point out the error and why am I gettin these errors.
Private Sub cmdCalculate_Click()
Dim uDT1 As...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |