It will work in a control that is bound to a text field, but not in a
control that is bound to a number field, because Access will automatically
reject the non-numeric value you entered before your code has a chance to
evaluate and replace it.
What you could do, to use this method and store the result, is to use an
unbound control, load the control with the field's value in the form's
Current event, and then use the control's AfterUpdate event to calculate the
result, update the control, *and* update the field. The actual number field
would have to be part of the form's recordsource, but not necessarily
displayed on the form.
Suppose your field was named "MyField", and you wanted to use the (unbound)
control "txtMyField" to display and edit it. You'd have code something like
this:
'----- start of example code -----
Private Sub Form_Current()
Me.txtMyField = Me.MyField
End Sub
Private Sub txtMyField_AfterUpdate()
On Error Resume Next
With Me.txtMyField
If Not IsNull(.Value) Then
.Value = Eval(.Value)
If Err.Number <> 0 Then
.Value = CVErr(5)
Else
Me.MyField = .Value
End If
End If
End With
End Sub
'----- end of example code -----
Note that the unbound control txtMyField isn't going to "undo" the same way
a bound control would. This may or may not be a problem. If you need to
make it "undo" along with the bound controls on the form, you'll have to
write your own mechanism for saving the original value, recognizing the Undo
event, and restoring the original value.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)