addition with in a field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to be able to set focus in a field, then enter "3 + 4" and when
hitting "=", the value in that field will be "7"
 
Using the "=" would add a layer of complexity and reduce response time,
because you would have to use either the KeyPress or KeyDown events which
will have to evaluate every keystroke.

Here is a way that will give you the same results regardless of how the text
box (not a field, fields are in tables) is exited:
In the after update event of the text box:
Me.MyTextBoxName = Eval(Me.MyTextBoxName)
You will need to ensure you have an error handler in the event procedure,
because if a user enters a value that cannot be evaluated, it will cause an
error.
 
You will need to provide an unbound text box on your form where the user can
enter the calcuation. In its AfterUpdate event procedure, use Eval() to
evaluate the expression, and assign the result to your field.

Example:

Private Sub txtCalc_AfterUpdate
If IsNull(Me.txtCalc) Then
Me.[MyField] = Null
Else
Me.[MyField] = Eval(Me.txtCalc)
End If
End Sub

Note that you will need error handling in case the user enters something
that makes no sense--typically error 2431.
 
Back
Top