PDA

View Full Version : Solved: Looping sum



petedw
08-04-2005, 05:25 AM
Hey Guys,
The following piece of coding is meant to add up a number of formfields results and put the final result of the sum in another formfield. When i run it at the moment, it just puts the result of the last formfield that it looks at in there.
Any ideas what i need to do??

Many Thanks

Pete

Sub MoneyInSums()
iNum = 17
Do While ActiveDocument.Bookmarks.Exists("MIn" & iNum)
ActiveDocument.FormFields("TotalMoneyIn").Result = _
ActiveDocument.FormFields("TotalMoneyIn").Result + ActiveDocument.FormFields("Min" & iNum).Result
iNum = iNum + 5
Loop
End Sub

xCav8r
08-04-2005, 06:02 AM
I only see it looking in "Min". Maybe you should do a for each?

petedw
08-04-2005, 06:21 AM
Can you make an example around the basis of my code please??

xCav8r
08-04-2005, 06:32 AM
This sums the values in every field except TotalMoneyIn.

Dim sngAddItUp as Single
Dim MyFormField as FormField
For Each MyFormField in ActiveDocument.FormFields
If MyFormField.Name <> "TotalMoneyIn" then
sngAddItUp = sngAddItUp + MyFormField.Result
End If
Next MyFormField
ActiveDocument.FormFields("TotalMoneyIn").Result = sngAddItUp

MOS MASTER
08-04-2005, 10:24 AM
Pete, :yes

You can set the properies of the Formfield to make the calculation.

So my question to you is do the users fill in the numbers by hand or are you filling it in by code?

If by hand then I would just set the field options to calculate on exit and make one formfield a formula field for the total! :whistle:

petedw
08-05-2005, 05:12 AM
The users fill in the formfields by hand but..... the form never has the same number of formfields on it that add together to make the total. I need an "if exists then add to the total" loop. There could b anything from 1 to unknown number of formfields that to need to be added to the total

MOS MASTER
08-05-2005, 09:46 AM
I don't get this!

Marco's code loops through all formfields in the document no need to check if it exists...It's in the collection!

So tell me why is his sub not working for you?
Perhaps a example would help of the problem. :whistle:

petedw
08-05-2005, 01:33 PM
I mixed what i already had with what Marco's coding to get


Sub MoneyInSums()
Dim SubTotal As Single
Dim iNum As Integer
iNum = 17
SubTotal = 0
Do While ActiveDocument.Bookmarks.Exists("MIn" & iNum)
SubTotal = SubTotal + ActiveDocument.FormFields("Min" & iNum).Result
iNum = iNum + 5
Loop
ActiveDocument.FormFields("TotalMoneyIn").Result = SubTotal
End Sub
That sorted it for me, cheers guys :beerchug:

Pete

MOS MASTER
08-05-2005, 02:31 PM
Hi Pete, :yes

Well the only change I see is the dimension of a Single variable...but who cares....the problem is solved and that's what it's all about! :whistle: