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

Formula returns error I need to assign value 0 to #Error

In my form i have two fields say fld1 and fld2. fld1 have data like 1,2,3,8,20. now I need to calculate the length of fld1 and assign it to fld2 he in this example fld2 must have 5. I used

=Len([fld1])-Len(Replace([fld1],",",""))+1
to do that but when fld1 contains null value it returns an error in fld2 like "#Error". I need 0 in fld2 if theres no value in fld1


help me please to solve this
Aug 2 '08 #1
12 1867
Stewart Ross
2,545 Expert Mod 2GB
Hi. You can use the Nz function to return an empty string in place of the null value for fld1:
Expand|Select|Wrap|Line Numbers
  1. =Len(Nz([fld1]))-Len(Replace(Nz([fld1]),",",""))+1
This will resolve the immediate errors. However, it is better to guard against empty strings and nulls explicitly, and in the long run it would be simpler to replace the above with a user-defined function in VBA which you could call.

As I do not know the purpose of your expression I will leave it for the moment, but I would advise as a general principle that once expressions start to get complicated it is worth considering writing a custom function to return the value instead of trying to do so in an in-line expression.

By the way, the length of the string without the commas is 6, not 5, in the example you give ("1,2,3,8,20" -> "123820". Length 6.).

-Stewart
Aug 2 '08 #2
missinglinq
3,532 Expert 2GB
He doesn't really mean the "length" of the string as in Len(string) Stewart. He has a string made up of dates that a person has missed from work during a given month, i.e. "01, 11,14,15,16" and he wants a month's total of the number of days, in this case 5.

Using Nz() won't work here, because even if the field is null, you'll still get 1 as the number of absences. He'll need to check for null first, then act accordingly:
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull([fld1]) Then
  2.   MonthTotal = Len([fld1]) - Len(Replace([fld1], ",", "")) + 1
  3. Else
  4.   MonthTotal = 0
  5. End If
Linq ;0)>
Aug 2 '08 #3
ADezii
8,834 Expert 8TB
He doesn't really mean the "length" of the string as in Len(string) Stewart. He has a string made up of dates that a person has missed from work during a given month, i.e. "01, 11,14,15,16" and he wants a month's total of the number of days, in this case 5.

Using Nz() won't work here, because even if the field is null, you'll still get 1 as the number of absences. He'll need to check for null first, then act accordingly:
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull([fld1]) Then
  2.   MonthTotal = Len([fld1]) - Len(Replace([fld1], ",", "")) + 1
  3. Else
  4.   MonthTotal = 0
  5. End If
Linq ;0)>
Couldn't you use the following formula to get the number of Absenses providing [fld1] is not Null?
Expand|Select|Wrap|Line Numbers
  1. UBound(Split(Me![fld1], ",")) + 1)
Aug 2 '08 #4
missinglinq
3,532 Expert 2GB
You could if you removed the final right-handed parenthesis!

Linq ;0)>
Aug 2 '08 #5
ADezii
8,834 Expert 8TB
You could if you removed the final right-handed parenthesis!

Linq ;0)>
Just wanted to see if you were paying attention! (LOL).
Aug 2 '08 #6
missinglinq
3,532 Expert 2GB
That's what my trig teacher always said when we caught him out!

Linq ;0)>
Aug 2 '08 #7
He doesn't really mean the "length" of the string as in Len(string) Stewart. He has a string made up of dates that a person has missed from work during a given month, i.e. "01, 11,14,15,16" and he wants a month's total of the number of days, in this case 5.

Using Nz() won't work here, because even if the field is null, you'll still get 1 as the number of absences. He'll need to check for null first, then act accordingly:
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull([fld1]) Then
  2.   MonthTotal = Len([fld1]) - Len(Replace([fld1], ",", "")) + 1
  3. Else
  4.   MonthTotal = 0
  5. End If
Linq ;0)>
Hi Friend this also returns Error. note that I am using Access form control to evaluate and not using any Front end like VB VC++..... help me pls
Aug 4 '08 #8
NeoPa
32,556 Expert Mod 16PB
Harid,

If you ignore certain posts then it will be harder to help you (ignoring the manners for now).

Is there a reason why the modified version (without the trailing closing parenthesis) of ADezii's code in post #4 will not work for you?
Aug 4 '08 #9
Harid,

If you ignore certain posts then it will be harder to help you (ignoring the manners for now).

Is there a reason why the modified version (without the trailing closing parenthesis) of ADezii's code in post #4 will not work for you?

Hi NeoPad

Sorry to say that i didn't get that i don't know where it went wrong but I got what i wanted from Stewart Ross Inverness post ie the 1st post I just modified the code and added IIF()

=IIf(IsNull([fld1]),"0",(Len([fld1])-Len(Replace([fld1],",",""))+1))


this works fine for me
anyway thanx for all who provided me the information
Aug 4 '08 #10
NeoPa
32,556 Expert Mod 16PB
Interesting.

Is what you're after actually a count of items as proposed (by MissingLinq in post #3) or is it actually the count of all digits? The solution you've gone for seems to be the latter.

IE. With the following data - which answer would you be looking for?
1,5,8,20,22,3,15
  1. 7
  2. 10
Aug 4 '08 #11
Interesting.

Is what you're after actually a count of items as proposed (by MissingLinq in post #3) or is it actually the count of all digits? The solution you've gone for seems to be the latter.

IE. With the following data - which answer would you be looking for?
1,5,8,20,22,3,15
  1. 7
  2. 10

Hi NeoPa

Here with given example I am Looking for the Answer A ie 7
Actually my case is I have a Table Leave in which 12 months are there Now I need to add the no: of leaves takes by each person on each month ie if a person takes 4 leaves in January like the followin dates 02,15,16,29 . I need January's total and I need 4 as my answer. hope u understood my situation
Aug 5 '08 #12
NeoPa
32,556 Expert Mod 16PB
I think you're missing the point somewhat Harid. I wasn't looking for a fuller description of your overall problem. I was trying to simplify the question, and get at least that part clear.

This thread is a perfect example of how much time and effort can be wasted when the question is not clear. Most of the posters (even Stewart I suspect from his comment) were focused on answering a different question than the one you intended.

Where Stewart got the answer right was simply in changing your existing code (which is quite a clever, if hard to read, way of counting the fields) to handle the situation where [Fld1] has a Null value. It's clear he still thought you were counting the digits rather than the number of separate numbers in the list.

Unfortunately, if [Fld1] can be Null, or even if it needs to be worked out in SQL code rather than in VBA, the clever solution that ADezii provided cannot work (unless embedded with some extra code in a public function).
Aug 5 '08 #13

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

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
3
by: JMCN | last post by:
does anyone know how to take out {enter} strings from a field? for instance, i have: "INTWEST ADJUSTMENT ORG ILLINOIS FUND PARTICIPANT DIVIDEND". i managed to take out extra spaces but then it...
7
by: Leon Shaw | last post by:
Need help understanding? Server Error in '/solo' Application. ---------------------------------------------------------------------------- ---- Object must implement IConvertible....
2
by: alex | last post by:
I need a more advanced formula than just an average for calculating items rating. I have: raitng value is on scale 1 to 10. s - sum of all ratings for an item n - number of rates (votes)
2
by: carmen | last post by:
i have the folowing code Dim y As New frmCobranz Dim MyReport As New RPTCobranz MyReport.DataDefinition.RecordSelectionFormula = "{CobH.RelNbr}= y.txtNoRelac.Text...
11
by: MLH | last post by:
I have 2 lines in a procedure that assign MyVariant a value - line #238 and line #491. When line #238 runs, the value is 152. When line #491 runs, the DLookup function returns Null. I would expect...
8
by: GB | last post by:
Hello, How to calculate value for the following formula (I need C# code): res = (((m+1)(m+2)...(m+(k-1)))/1.2...(k-1)) or more generalized formula is: k-1 __ | | (m+i)
0
by: mahibang | last post by:
Hi Guys.! Thank u in advance for ur reply. Pls see the attachment. Hope i've explained the issue better. I've the problem in proposing an interface and programming too.... As shown in...
1
by: Monusonu | last post by:
Hi Expert, I am trying to get the value from excel formula cell using POI. My code works fine for less complex formula cells, but fails or returns error code for complex formula cells. Following...
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: 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
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
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,...
0
isladogs
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...

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.