Sum textboxes in a collection

M

Mark

Any help for a newbie would be appreciated. I'm trying to sum several
texbox inputs on a form and display the result in a "totals" textbox on the
same form. The textboxes are part of a collection.

The following code concatenates the values in the textbox inputs rather than
adding the values:
Global declarations:
Dim textBoxes as New Collection
Dim textboxNames() as String

Private Sub txtCash_AfterUpdate()

'update total allocation
AllocationTotal

End Sub


Sub AllocationTotal()
Dim i As Integer
Dim fieldVal, boxSum As Variant

txtTotalAllocation.Value = 0

For i = 2 To (totalFields - 1)

fieldVal = textBoxes(textboxNames(i - 1)).Text
boxSum = boxSum + fieldVal

Next 'i

txtTotalAllocation.Value = boxSum

End Sub

How can I change this code so that the values in each textbox are summed
instead of concatenated?

Thanks in advance,
Mark
 
T

Tom Ogilvy

Sub AllocationTotal()
Dim i As Integer
Dim fieldVal, boxSum As Double

txtTotalAllocation.Value = 0

For i = 2 To (totalFields - 1)

fieldVal = textBoxes(textboxNames(i - 1)).Text
if isnumeric(fieldVal) then
boxSum = boxSum + cdbl(fieldVal)
end if

Next 'i

txtTotalAllocation.Value = Format(boxSum,"#.00")

End Sub

If they will be integers, then change Double to Long, cdbl to clng
Format to Format(boxSum,"0")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top