[AccessD] add numbers

Stuart McLachlan stuart at lexacorp.com.pg
Tue Apr 12 07:18:43 CDT 2005


On 12 Apr 2005 at 22:03, Stuart McLachlan wrote:

> That said, this one does work with multi digit numbers: 
...
>          While IsNumeric(Mid$(InputString, lngLoopcount, 1))

I just tried this against Pedro's original examples and it broke on 
"2(2v+2a)". It returned 8 instead of 6.

The problem is that Val("+2a)") evaluates to 2, but IsNumeric("+") is 
false. So it adds 2 when it gets to the "+" and again when it gets to the 
"2"

This version handles "+2" and "-2" etc in the string as well:

Function Addnumbers(InputString As String) As Double
   Dim lngStore As Double
   Dim lngLoopcount As Double
   lngLoopcount = 0
   Do
     lngLoopcount = lngLoopcount + 1
         lngStore = lngStore + Val(Mid$(InputString, lngLoopcount))
         'move past current number
         While InStr("123456789+-", Mid$(InputString, lngLoopcount, 1)) > 0
             lngLoopcount = lngLoopcount + 1
         Wend
   Loop Until lngLoopcount >= Len(InputString)
   Addnumbers = lngStore
End Function


-- 
Stuart





More information about the AccessD mailing list