Calculator within textbox (1 Viewer)

nhorton79

Registered User.
Local time
Today, 20:09
Joined
Aug 17, 2015
Messages
147
Hi All,

Wanting to have a textbox be able to perform calculations prior to saving.

We are often working with square meterage of products in our quantity textbox (txtQty). So would like for users to be able to enter a calculation and have this evaluate prior to updating.

e.g. typing in 10*4/10 would result in 4

I figured that I could maybe put some VBA in the BeforeUpdate event of the textbox using the Eval function.

I've tried entering:

Me.txtQty = Eval(Me.txtQty)

But this doesn't work. Error being the value you entered isn't valid for this field.

Any ideas? Has anyone done anything like this before?
 

plog

Banishment Pending
Local time
Today, 03:09
Joined
May 11, 2011
Messages
11,653
I have not nor would not do this--I've had users misspell their own names, no way I trust them to input valid calculations. However, I essentially got your code to work.

I made a form with a button and an input, then put this code on the button:

Code:
Private Sub ButtonName_Click()

str_Calculation = Eval(Me.InputValue)
MsgBox (str_Calculation)

End Sub

I copied and pasted your calculation and it gave me the right result. Perhaps you can post your code.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:09
Joined
May 21, 2018
Messages
8,554
 

nhorton79

Registered User.
Local time
Today, 20:09
Joined
Aug 17, 2015
Messages
147
Sorry if my question was a little vague. Just reread it again.

Users usually just use calculator in windows to get the figure to enter but want to find a way for them to just input directly into the field.

So trusting their input anyway.

I can get it to work by msgbox or printing to immediate window but can’t get it to change the value in the textbox (which is bound).

I suppose I could use a popup calculator as suggested and then populate the textbox from the output of that, just wanted it to be done in the textbox itself but appears there is no event I can use to alter this before saving/updating as beforeupdate event won’t let you change the value.

oh well. Thanks anyway.
 

June7

AWF VIP
Local time
Today, 00:09
Joined
Mar 9, 2014
Messages
5,488
The input has to be to UNBOUND textbox. Then code saves to BOUND textbox.
 

Users who are viewing this thread

Top Bottom