Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Calculating a Value based on a formula typed into a record.

Posted on 2012-08-27
Medium Priority
277 Views
Hi all, trust you are keeping well.

I have a table that has say the following fields.
Var1
Var2
Var3
cFormula

CFormula is a text field with for example: "=Var1*Var2*Var3"
or
CFormula could have: "=3.141593*Var2"

The idea being that I can create a library of formulas that would use "generic" fields.
The user then chooses the formula they want and it works out the result.

I would like some assistance (if possible) on how I can get VBA to actually "do" the formula stored in the CFormula field?

Thanks
Sean
0
Question by:SeanNij

LVL 13

Accepted Solution

Ryan earned 2000 total points
ID: 38336280
you can do a simple string replace and the Evaluate()

populate rs as recordset

str=rs!cFormula
str=replace(str,"Var1",rs!var1)
str=replace(str,"Var2",rs!var2)
str=replace(str,"Var3",rs!var3)
value=Evaluate(str)

Certainly this code can be cleaned up, and could be a single line if you want.
value=Evaluate(replace(replace(replace(rs!cFormula,"Var1",rs!var1),"Var2",rs!var2),"Var3",rs!var3))
0

LVL 1

Author Comment

ID: 38336320
Fantastic!!!
Called Eval() in Access 2010 - not Evaluate()

Thank you!
0

LVL 40

Expert Comment

ID: 38336337
You also can create a table with list of possible formula:
ID Function
1   A*B
2   B-C
and function like this:
``````Public Function calcf(F As Long, Optional ByVal A, Optional ByVal B, Optional ByVal C) As Variant
Select Case F
Case 1
calcf = A * B
Case 2
calcf = B - C
Case Else
calcf = "Error"
End Select
End Function
``````
Create combobox where user can select formula and send it to function with values from other fields
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
###### Suggested Courses
Course of the Month20 days, 22 hours left to enroll