Math Function from Field Before Update

D

Doctor

Have any of you written a function that you would be willing to share that
performs math on text entered into a field before it is stored in the table?

For instance, it would be helpful for a user to enter quantities of
inventory into a field like 45+56+48 or 14+2*15. Then the function would
calculate the total to be returned to the field to be saved.

I'm sure I could come up with something, but I'm equally sure that someone
has already invented this "wheel" before.

Thanks in advance.
 
B

Banana

Doctor said:
Have any of you written a function that you would be willing to share that
performs math on text entered into a field before it is stored in the table?

For instance, it would be helpful for a user to enter quantities of
inventory into a field like 45+56+48 or 14+2*15. Then the function would
calculate the total to be returned to the field to be saved.

I'm sure I could come up with something, but I'm equally sure that someone
has already invented this "wheel" before.

Thanks in advance.

A lazy way to implement this would be to use Eval() function in
textbox's AfterUpdate.

However, it's quite dangerous because the user can then type in whatever
is valid code and thus do arbitiary code execution, so I'd invest in a
bit extra work to restrict what are allowed for this operation.

Off the top of head, I'd write a series of function to replicate an
operator (e.g. Public Function AddIt(InputOne, InputTwo) As Integer 'Or
Double? Or Currency?), then have a function to parse the string and send
it to each the function.

Would love to know if someone already invented the wheel as well...
 
J

Jack Leach

?Eval(45+45)
90

?Eval("200*4")
800

So the Eval function will probably come into play here. But I don't think
you can change the value of the control in BeforeUpdate, you might have to do
it on Exit or LostFocus.

Also, you may have to do some typecasting to get your table to accept it.


hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
M

Marshall Barton

Doctor said:
Have any of you written a function that you would be willing to share that
performs math on text entered into a field before it is stored in the table?

For instance, it would be helpful for a user to enter quantities of
inventory into a field like 45+56+48 or 14+2*15. Then the function would
calculate the total to be returned to the field to be saved.

I'm sure I could come up with something, but I'm equally sure that someone
has already invented this "wheel" before.


I've always used Eval to do that. To prevent potentially
dangerous function calls, check to make sure the expression
contains only numbers and operators:

If txtQty Like "*[!0-9+-*/^.() ]*" Then
Beep
Else
Me.txtQty = Eval(Me.txtQty)
End If

Note that can still produce errors if the user entered
expression is not legal (e.g. 2///3) so use error trapping
to catch those things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top