Reputation: 11
I've got a form that I'd like users to be able to enter simple maths into, with the calculated value stored in the table.
For example:
Currently, entering "1+5" in my numeric form field results in an error re: 'entering text into a numeric field'. Is there any way to do this?
Upvotes: 1
Views: 1983
Reputation: 2595
You can write a VBA script to parse the input and perform any needed calculations. This could be triggered by an unbound form field's BeforeUpdate event.
Steps:
Note that, for reasons of security and/or data integrity, you may need to sanitize your user input before evaluating it as an expression. The details of "how" and "why" fall outside the scope of the original question.
Private Sub Input1_BeforeUpdate(Cancel As Integer)
' TO-DO: Validate/sanitize input as necessary for security & validity;
' Exit Sub prior to the Eval() if we see something we don't like
Dim result As Variant
result = Eval(Input1.Value) ' Evaluate the contents of Input1
If IsNumeric(result) Then Field1 = result ' Save any numeric result to Field1
End Sub
Upvotes: 3