• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 803
  • Last Modified:

A quick Sum button script

I need a quick lotus script that sums three number fields.  Putting it in a button
0
Jaziar
Asked:
Jaziar
  • 5
  • 5
  • 2
  • +2
1 Solution
 
qwaleteeCommented:
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
 
Jean Marie GeeraertsCommented:
OK. This should be fairly simple.
But before we can help you, we'd need some extra info:
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
 
p_parthaCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sjef BosmanGroupware ConsultantCommented:
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
 
qwaleteeCommented:
Interesting.  Jerrith's answer is teh LS version of my @code... except there's no checking for blank numbers.
0
 
JaziarAuthor Commented:
Qwaletee - I did try your answer.  I could not get it to work correctly.  I am sure it was something I did.
0
 
Jean Marie GeeraertsCommented:
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
 
qwaleteeCommented:
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
 
Jean Marie GeeraertsCommented:
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
 
qwaleteeCommented:
WHat's the M in JM?
0
 
p_parthaCommented:
Qwal,

I believe it's Jean Marie

-Partha
0
 
Jean Marie GeeraertsCommented:
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
 
qwaleteeCommented:
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
 
Jean Marie GeeraertsCommented:
Too bad. I miss our occasional chats.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now