how to calculate value in textbox (1 Viewer)

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
Hi all, I would like to ask if it is possible to calculate this type of value :
20+ 2+ 2 in textbox?

Like for now, I have two textbox .
Textbox A has value : 20 + 2 +2
Then textbox box b has value of 20 , then i want to add textbox A value to textbox b.
This means that textbox b = 44, [20 + 20 + 2 + 2]
 

Minty

AWF VIP
Local time
Today, 22:11
Joined
Jul 26, 2013
Messages
10,371
Yes, although it is highly not advised - Access is not Excel.
If they are distinct values they should be stored separately.

What have you tried? Research the Eval() method.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:11
Joined
May 7, 2009
Messages
19,230
use Eval() function:

textB=Eval(textA)
 

Solo712

Registered User.
Local time
Today, 17:11
Joined
Oct 19, 2012
Messages
828
Hi all, I would like to ask if it is possible to calculate this type of value :
20+ 2+ 2 in textbox?

Like for now, I have two textbox .
Textbox A has value : 20 + 2 +2
Then textbox box b has value of 20 , then i want to add textbox A value to textbox b.
This means that textbox b = 44, [20 + 20 + 2 + 2]

You can't have two values in textbox b. You'd have to create an event (Button click ?) that modifies textbox b, like
Code:
textboxB = textboxB + Eval(textboxA)

Best,
Jiri
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:11
Joined
Feb 28, 2001
Messages
27,172
Note further that if text box B is BOUND, then you cannot do this without altering the underlying record because binding generally limits what you can do with the contents of a control.
 

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
You can't have two values in textbox b. You'd have to create an event (Button click ?) that modifies textbox b, like
Code:
textboxB = textboxB + Eval(textboxA)

Best,
Jiri

Yes, i do have a button to click and calculate textbox B
 

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
Hi all, I tried the method you guys said, but i am still getting error.
Below is my code:
Code:
If Me.txtLbs <= 130 Then
Me.txtGrossweight = (Me.txtNettweight + 20) + Eval(Me.txtBuyerRequirement)

I have also attached a image of my form and the error
 

Attachments

  • Untitled.png
    Untitled.png
    31.7 KB · Views: 53

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:11
Joined
Feb 28, 2001
Messages
27,172
That is a datatype error for storing data in a field.

This means that the txtGrossweight control is BOUND and one or the other of the other controls is of a different data type (or the expression is being forced to a different data type) than the target field (that is associated with the control).
 

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
That is a datatype error for storing data in a field.

This means that the txtGrossweight control is BOUND and one or the other of the other controls is of a different data type (or the expression is being forced to a different data type) than the target field (that is associated with the control).

Nope, txtGrossweight is not bound as i will need to use button to display the value
 

Minty

AWF VIP
Local time
Today, 22:11
Joined
Jul 26, 2013
Messages
10,371
I can't help feeling this would be better achieved in the underlying forms query.
 

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
I can't help feeling this would be better achieved in the underlying forms query.

The problem now is that i am not able to key in "+" sign in my textbox
 

Minty

AWF VIP
Local time
Today, 22:11
Joined
Jul 26, 2013
Messages
10,371
I don't understand why you would want this functionality. You can add 2 or 10 control boxes up in either code or a query, why try and build a calculator into access?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:11
Joined
Feb 28, 2001
Messages
27,172
Nope, txtGrossweight is not bound as i will need to use button to display the value

A totally unbound text box will not do this unless you have set constraints on the format of the box. That error is a data type mismatch where the computation produces something with a data type that is inconsistent with the type of the destination.
 

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
I don't understand why you would want this functionality. You can add 2 or 10 control boxes up in either code or a query, why try and build a calculator into access?

As this is one of the requirement given by the user. Hence, i have to try to get it done. Well, you said this can be done in either code or query, with that can the user still key in the the value they want in the textbox on the form?
 

luzz

Registered User.
Local time
Today, 14:11
Joined
Aug 23, 2017
Messages
346
A totally unbound text box will not do this unless you have set constraints on the format of the box. That error is a data type mismatch where the computation produces something with a data type that is inconsistent with the type of the destination.

does that mean i will need to change the data type?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:11
Joined
Feb 28, 2001
Messages
27,172
does that mean i will need to change the data type?

Somewhere along the line, yes. You can probably typecast or format the expression before trying to store it. If you are not familiar with typecasting, it really is just applying one of several possible functions to change an element from one data type to another. Like using CDbl(x) to make X appear in DOUBLE format. Or CStr() to convert something to a string. There are many other such functions as well.

I'm betting you need a string in your text box, so CStr() is probably what you will need. But I could be wrong. Try a few things and see what Access likes. It is, after all, the sole arbiter of syntax.
 

Minty

AWF VIP
Local time
Today, 22:11
Joined
Jul 26, 2013
Messages
10,371
As this is one of the requirement given by the user. Hence, i have to try to get it done. Well, you said this can be done in either code or query, with that can the user still key in the the value they want in the textbox on the form?

My comment is that if these are values are already somewhere in the database then you can do the calculations behind the scenes.

If they aren't the they probably should be stored so the end value can be calculated. That is how a database works. Data in - Manipulated Data out.

https://www.youtube.com/watch?v=l5vsqWPdiLE
 

Users who are viewing this thread

Top Bottom