
Originally Posted by
Rawb
Sorry, I tend to get a little long-winded sometimes. . .
I'm not looking to add strings together.
What I have is an equation saved as a string, complete with plus and minus signs. I want to be able to "parse" that string and figure out what the saved equation equals.
For example: I have a string "4+6". I'm looking for a way give that string to Access and have it able to figure out that "Oh hey, that's math!" and then just tell me that the answer is "10".
the only way I know is with a UDF. For instance:
Code:
Option Compare Database
Option Base 1
Function CalcEquation(inputFormula As String) As Long
Dim lastOpPos As Long
Dim elNum As Long
Dim i As Long
Dim arrMath() As Variant
inputFormula = "+" & inputFormula
elNum = 1
lastOpPos = 1
For i = 2 To Len(str)
If Not IsNumeric(Mid(str, i, 1)) Then
ReDim Preserve arrMath(elNum)
arrMath(elNum) = Mid(str, lastOpPos, i - lastOpPos)
lastOpPos = i
elNum = elNum + 1
End If
Next i
For i = LBound(arrMath) To UBound(arrMath)
CalcEquation = CLng(CalcEquation) + CLng(arrMath(i))
Next i
End Function
This can be condensed even further without the use of the array. What this does though is take the portions out as integers (positives or negatives), converts them to LONGS and adds them together with the + operator, which is basically just doing math. Adding numerical types with the "+" sign results in math calculation, but doing it on strings results in simple concatenation.