Kckronic21
Programmer
Hi, I want to know what is the best wat to set up a query for calculating simple algebraic equations like the this one for example: 3X + 15 = 45. Thanks!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
x:(Field3 - Field2)/Field1
=(Field3 - Field2)/Field1
Public Function basStacMath(ParamArray FctList() As Variant) As Double
Dim Idx As Long
Dim MyStr As String
'SAMPLE Usage:
'? basStacMath("(", "45", "+", "15", ")", "/", "3")
'20
Idx = 0
Do While Idx <= UBound(FctArray())
MyStr = MyStr & FctArray(Idx) & " "
Idx = Idx + 1
Loop
basStacMath = Eval(FctList)
End Function
Public Function basStackEval(ProbId As Long) As Double
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSql As String
Dim Quo As String
Dim MyStr As Variant
'Sample Usage:
'? basStackEval(1)
' 10
'Assumes:tblStackOper ~ as Below.
'StackOper PrbStep Problem
'( 1 1
'45 2 1
'- 3 1
'15 4 1
') 5 1
'/ 6 1
'3 7 1
Quo = Chr(34)
strSql = "Select StackOper, PrbStep From tblStackOper "
strSql = strSql & "Where Problem = " & ProbId & " "
strSql = strSql & "Order By PrbStep " & ";"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSql, dbOpenDynaset)
Do While Not rst.EOF
MyStr = MyStr & rst!StackOper & " "
rst.MoveNext
Loop
basStackEval = Eval(MyStr)
End Function
Public Function basEvalMulti(Fct As String, _
MinX As Single, _
MaxX As Single, _
StepIncr As Single) As Variant
'Sample Usage:
'm * (X) + b = 2 * X + 5
'Where: 3.21 <= X <= 7.67, Incr @ 0.25
Dim MyX As Single
Dim MyVals() As Variant
Dim MyFct As String
Dim strMyX As String
Dim FctParts As Variant
ReDim MyVals(((MaxX - MinX) / StepIncr) + 1)
FctParts = basSplit(Fct, "X")
Idx = 0
MyX = MinX
Do While MyX <= MaxX
strMyX = CStr(MyX)
MyFct = Join(FctParts, strMyX)
'MyFct = Replace(Fct, "X", strMyX)
MyVals(Idx) = Eval(MyFct)
MyX = MyX + StepIncr
Idx = Idx + 1
Loop
basEvalMulti = MyVals
End Function
Public Function TestMultiRepl()
RtnVal = basEvalMulti("2 * X + 5", 3.21, 7.67, 0.25)
Idx = 0
While Idx <= UBound(RtnVal)
Debug.Print Idx, RtnVal(Idx)
Idx = Idx + 1
Wend
End Function
[code] MichaelRed
m.red@att.net
There is never time to do it right but there is always time to do it over
'Declaration
Type MyFctType
MyFct As String
MinX As Single
MaxX As Single
StepIncr As Single
End Type
Dim MyFct(10) As MyFctType
[code]
'__________________________________________________
[code]
'Function
Public Function TestMultiEvalLoop()
MyFct(0).MyFct = "2 * X + 5"
MyFct(0).MinX = 3.21
MyFct(0).MaxX = 7.67
MyFct(0).StepIncr = 0.25
MyFct(1).MyFct = "2 * X * X + 5 * X + 3"
MyFct(1).MinX = 1.23
MyFct(1).MaxX = 8.88
MyFct(1).StepIncr = 0.22
Jdx = 0
Do While Jdx < UBound(MyFct)
If (MyFct(Jdx).MyFct <> "") Then
RtnVal = basEvalMulti(MyFct(Jdx).MyFct, MyFct(Jdx).MinX, MyFct(Jdx).MaxX, MyFct(Jdx).StepIncr)
Debug.Print "Evaluating " & MyFct(Jdx).MyFct
Idx = 0
While Idx <= UBound(RtnVal)
Debug.Print Idx, RtnVal(Idx)
Idx = Idx + 1
Wend
Debug.Print
End If
Jdx = Jdx + 1
Loop
End Function