Solved

# A quick Sum button script

Posted on 2005-05-11
779 Views
I need a quick lotus script that sums three number fields.  Putting it in a button
0
Question by:Jaziar

LVL 31

Expert Comment

value1 := @TextToNumber(@Text(fieldName1));
value2 := @TextToNumber(@Text(fieldName2));
value3 := @TextToNumber(@Text(fieldName3));
number1 := @If(@IsError(value1); 0; value1);
number2 := @If(@IsError(value2); 0; value2);
number3 := @If(@IsError(value3); 0; value3);
total := number1 + number2 + number3;
@Promtp([OK]; "Total"; @Text(total));
0

LVL 8

Accepted Solution

OK. This should be fairly simple.
1. Where do you want the button? In a view, on the opened document
2. What do you want to do with the result? Just display it? Update a field with the sum? ...
3. Is this a web application, notes client application?

Example of simple code to just display the sum of three fields on the currently opened document, using LotusScript.
Code is intended for document opened in read mode and will read info from backend document.

Sub Initialize
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim dblSum As Double

Set uidoc = ws.CurrentDocument 'retrieve the current UI Document
Set doc = uidoc.Document 'retrieve backend document from the UI Document
dblSum = doc.Field1(0) + doc.Field2(0) + doc.Field3(0)

Messagebox "The sum of " & doc.Field1(0) & ", " & doc.Field2(0) & " and " doc.Field3(0) & " equals " dblSum
End Sub
0

LVL 14

Expert Comment

From one of the good answers from sjef:

a:= 4;
b:= 10;
c:= "";
d:= 6;
@Sum(@TextToNumber(@Trim(@Text(a):@Text(b):@Text(c):@Text(d))))

- Partha
0

LVL 46

Expert Comment

Hi Partha,

> From one of the good answers from sjef:
Now how am I supposed to read this? Where is the emphasis in that line? Is it
From ONE of the good answers from sjef
or
From one of the GOOD answers from sjef

Kiddin'... Can't top that.

Cheers!
Sjef :)
0

LVL 31

Expert Comment

Interesting.  Jerrith's answer is teh LS version of my @code... except there's no checking for blank numbers.
0

Author Comment

Qwaletee - I did try your answer.  I could not get it to work correctly.  I am sure it was something I did.
0

LVL 8

Expert Comment

Didn't see your post before I posted qwaletee, so no translation to LS if this is what you are implying :-)
Anyway, with LotusScript it should automatically convert the values to numbers. To do it explicitly, you could change this line :

dblSum = doc.Field1(0) + doc.Field2(0) + doc.Field3(0)

to :

dblSum = Cdbl(doc.Field1(0)) + Cdbl(doc.Field2(0)) + Cdbl(doc.Field3(0))

which would explicitly transfer the value (or empty value) of the fields to a double number.

The '&' in the messagebox makes sure that everything is automatically translated to text so it can be put in the messagebox.

0

LVL 31

Expert Comment

It will convert text-storage numbers to numeric-storage numbers.  The problem you run into is when a numeric field is blank.  Notes stores numeric fields as numeric if there is a number present, but also allows "null" values, which are empty text strings.

If you try the following operation in LS:
Dim x as <numeric type>
x = "3" + "4"
What you will get is numeric value 34, not 7.  That's because there are two ways to interpret + addition or string concatenation).  The expression "3" + "4" appears to be a string expression, so it becomes sring concatenation of 3 joined to 4 for "34" and teh string "34" is converted during assignment to the numeric variable x.

Now, if you try to add 3 + "" + 4 (wheer 3 and 4 are numeric expressions, not string), you will get a type mismatch, because the expression appears to be numeric (starts with 3, a numeric expression itself), and the plus becomes an addition perator which is incompatible with the empty string following.

Field values in Notes are considered arrays of variants by the object model, so Field1(0) returns a variant of type number or a variat of type string, depending on whether the value is filled in.  Since it could be string, you could end up with a type mismatch error.
0

LVL 8

Expert Comment

Hence the correction with Cdbl :-)

Just for clarification: I know all that, but was just writing a quick and dirty sample script. Had expected an answer to my questions and then would have perfected the script, but apparantly the sample was good enough for jaziar to find the solution to his problem.

Regards,
JM
0

LVL 31

Expert Comment

WHat's the M in JM?
0

LVL 14

Expert Comment

Qwal,

I believe it's Jean Marie

-Partha
0

LVL 8

Expert Comment

Yep, exactly right. That's my name.
So have any of you heard anything from the 'old gang', like Arun and zvonko?

I'm not that active in the community myself for the time being, because I'm quite busy at work and don't really have the time to browse around EE anymore.
But from time to time I try to have a quick look and see what's happening at EE.

After all, I did learn a lot from it.
0

LVL 31

Expert Comment

Zvonko has been doing volunteer cleanup, so I know he is active.  Arun has dropped out of site. Hemantha still contributes.  CRAK definitely lurkes (as page editor) and chimes in once in a while.  AJP, Scott, ghost... well, they are ghosts.
0

LVL 8

Expert Comment

Too bad. I miss our occasional chats.
0

## Featured Post

### Suggested Solutions

For Desktop Techs: How to retain a user's Notes configuration data when swapping out the end user's computer. (Assuming that you are not upgrading to a completely different version of Notes client) All you need to do is: 1) install Notes o…
Notes Document Link used by IBM Notes is a link file which aids in the sharing of links to documents in email and webpages. The posts describe the importance and steps to create a Lotus Notes NDL file in brief.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…