Using VBA to add values from one field to another

  • Thread starter Thread starter gollum
  • Start date Start date
G

gollum

Guest
I have an Access Database converted from '97 to 2000 where a text field can be entered as (E.g) 120+150+75 etc. Ihad some code that seemd to work in '97, but doesn't work in 2000, the information i.e the sum of the above is then entered into another field on the form, the original code is as follows:

Function SSum(X$)
Dim sum as integer
Dim V as integer
Dim y as String
sum=0
y=X$
while Len(y)>0
V=val(y)
sum=sum+V
y=Mid$(y,(Len(V)+2))
Wend
SSum=Sum
End Function

I can see where the problem is (can only put in 2 digit numbers, e.g. 12+25+30)
I don't have enough knowledge to figure out how to create the sum.

Help

Thanks

Gollum
 
Here, try this code. It's very similar to what you had, but it uses the Instr() function to skip past the "+" character. I've tested this and it seems to work with any length number, as long as the sum doesn't surpass the Integer variable limit of 32,767. Hope this helps. Good luck.

Function SSum(X$)
Dim sum As Integer
Dim V As Integer
Dim y As String

sum = 0
y = X$
While Len(y) > 0
V = Val(y)
sum = sum + V
If InStr(y, "+") <> 0 Then
y = Mid$(y, InStr(y, "+") + 1)
Else
y = ""
End If
Wend
SSum = sum
End Function
 

Users who are viewing this thread

Back
Top Bottom